qpnp-pin.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/interrupt.h>
  14. #include <linux/regmap.h>
  15. #include <linux/types.h>
  16. #include <linux/spmi.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/export.h>
  26. #include <linux/module.h>
  27. #include <linux/export.h>
  28. #include <linux/qpnp/pin.h>
  29. #define Q_REG_ADDR(q_spec, reg_index) \
  30. ((q_spec)->offset + reg_index)
  31. #define Q_REG_STATUS1 0x8
  32. #define Q_REG_STATUS1_VAL_MASK 0x1
  33. #define Q_REG_STATUS1_GPIO_EN_REV0_MASK 0x2
  34. #define Q_REG_STATUS1_GPIO_EN_MASK 0x80
  35. #define Q_REG_STATUS1_MPP_EN_MASK 0x80
  36. #define Q_NUM_CTL_REGS 0xD
  37. /* revision registers base address offsets */
  38. #define Q_REG_DIG_MINOR_REV 0x0
  39. #define Q_REG_DIG_MAJOR_REV 0x1
  40. #define Q_REG_ANA_MINOR_REV 0x2
  41. /* type registers base address offsets */
  42. #define Q_REG_TYPE 0x4
  43. #define Q_REG_SUBTYPE 0x5
  44. /* gpio peripheral type and subtype values */
  45. #define Q_GPIO_TYPE 0x10
  46. #define Q_GPIO_SUBTYPE_GPIO_4CH 0x1
  47. #define Q_GPIO_SUBTYPE_GPIOC_4CH 0x5
  48. #define Q_GPIO_SUBTYPE_GPIO_8CH 0x9
  49. #define Q_GPIO_SUBTYPE_GPIOC_8CH 0xD
  50. #define Q_GPIO_SUBTYPE_GPIO_LV 0x10
  51. #define Q_GPIO_SUBTYPE_GPIO_MV 0x11
  52. /* mpp peripheral type and subtype values */
  53. #define Q_MPP_TYPE 0x11
  54. #define Q_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
  55. #define Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
  56. #define Q_MPP_SUBTYPE_4CH_NO_SINK 0x5
  57. #define Q_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
  58. #define Q_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
  59. #define Q_MPP_SUBTYPE_8CH_FULL_FUNC 0xF
  60. /* control register base address offsets */
  61. #define Q_REG_MODE_CTL 0x40
  62. #define Q_REG_DIG_VIN_CTL 0x41
  63. #define Q_REG_DIG_PULL_CTL 0x42
  64. #define Q_REG_DIG_IN_CTL 0x43
  65. #define Q_REG_DIG_OUT_SRC_CTL 0x44
  66. #define Q_REG_DIG_OUT_CTL 0x45
  67. #define Q_REG_EN_CTL 0x46
  68. #define Q_REG_AOUT_CTL 0x48
  69. #define Q_REG_AIN_CTL 0x4A
  70. #define Q_REG_APASS_SEL_CTL 0x4A
  71. #define Q_REG_SINK_CTL 0x4C
  72. /* control register regs array indices */
  73. #define Q_REG_I_MODE_CTL 0
  74. #define Q_REG_I_DIG_VIN_CTL 1
  75. #define Q_REG_I_DIG_PULL_CTL 2
  76. #define Q_REG_I_DIG_IN_CTL 3
  77. #define Q_REG_I_DIG_OUT_SRC_CTL 4
  78. #define Q_REG_I_DIG_OUT_CTL 5
  79. #define Q_REG_I_EN_CTL 6
  80. #define Q_REG_I_AOUT_CTL 8
  81. #define Q_REG_I_APASS_SEL_CTL 10
  82. #define Q_REG_I_AIN_CTL 10
  83. #define Q_REG_I_SINK_CTL 12
  84. /* control reg: mode */
  85. #define Q_REG_OUT_INVERT_SHIFT 0
  86. #define Q_REG_OUT_INVERT_MASK 0x1
  87. #define Q_REG_SRC_SEL_SHIFT 1
  88. #define Q_REG_SRC_SEL_MASK 0xE
  89. #define Q_REG_MODE_SEL_SHIFT 4
  90. #define Q_REG_MODE_SEL_MASK 0x70
  91. #define Q_REG_LV_MV_MODE_SEL_SHIFT 0
  92. #define Q_REG_LV_MV_MODE_SEL_MASK 0x3
  93. /* control reg: dig_out_src (GPIO LV/MV only) */
  94. #define Q_REG_DIG_OUT_SRC_SRC_SEL_SHIFT 0
  95. #define Q_REG_DIG_OUT_SRC_SRC_SEL_MASK 0xF
  96. #define Q_REG_DIG_OUT_SRC_INVERT_SHIFT 7
  97. #define Q_REG_DIG_OUT_SRC_INVERT_MASK 0x80
  98. /* control reg: dig_vin */
  99. #define Q_REG_VIN_SHIFT 0
  100. #define Q_REG_VIN_MASK 0x7
  101. /* control reg: dig_pull */
  102. #define Q_REG_PULL_SHIFT 0
  103. #define Q_REG_PULL_MASK 0x7
  104. /* control reg: dig_out */
  105. #define Q_REG_OUT_STRENGTH_SHIFT 0
  106. #define Q_REG_OUT_STRENGTH_MASK 0x3
  107. #define Q_REG_OUT_TYPE_SHIFT 4
  108. #define Q_REG_OUT_TYPE_MASK 0x30
  109. /* control reg: dig_in_ctl */
  110. #define Q_REG_DTEST_SEL_SHIFT 0
  111. #define Q_REG_DTEST_SEL_MASK 0xF
  112. #define Q_REG_LV_MV_DTEST_SEL_CFG_SHIFT 0
  113. #define Q_REG_LV_MV_DTEST_SEL_CFG_MASK 0x7
  114. #define Q_REG_LV_MV_DTEST_SEL_EN_SHIFT 7
  115. #define Q_REG_LV_MV_DTEST_SEL_EN_MASK 0x80
  116. /* control reg: en */
  117. #define Q_REG_MASTER_EN_SHIFT 7
  118. #define Q_REG_MASTER_EN_MASK 0x80
  119. /* control reg: ana_out */
  120. #define Q_REG_AOUT_REF_SHIFT 0
  121. #define Q_REG_AOUT_REF_MASK 0x7
  122. /* control reg: ana_in */
  123. #define Q_REG_AIN_ROUTE_SHIFT 0
  124. #define Q_REG_AIN_ROUTE_MASK 0x7
  125. /* control reg: sink */
  126. #define Q_REG_CS_OUT_SHIFT 0
  127. #define Q_REG_CS_OUT_MASK 0x7
  128. /* control ref: apass_sel */
  129. #define Q_REG_APASS_SEL_SHIFT 0
  130. #define Q_REG_APASS_SEL_MASK 0x3
  131. enum qpnp_pin_param_type {
  132. Q_PIN_CFG_MODE,
  133. Q_PIN_CFG_OUTPUT_TYPE,
  134. Q_PIN_CFG_INVERT,
  135. Q_PIN_CFG_PULL,
  136. Q_PIN_CFG_VIN_SEL,
  137. Q_PIN_CFG_OUT_STRENGTH,
  138. Q_PIN_CFG_SRC_SEL,
  139. Q_PIN_CFG_MASTER_EN,
  140. Q_PIN_CFG_AOUT_REF,
  141. Q_PIN_CFG_AIN_ROUTE,
  142. Q_PIN_CFG_CS_OUT,
  143. Q_PIN_CFG_APASS_SEL,
  144. Q_PIN_CFG_DTEST_SEL,
  145. Q_PIN_CFG_INVALID,
  146. };
  147. #define Q_NUM_PARAMS Q_PIN_CFG_INVALID
  148. /* param error checking */
  149. #define QPNP_PIN_GPIO_MODE_INVALID 3
  150. #define QPNP_PIN_GPIO_LV_MV_MODE_INVALID 4
  151. #define QPNP_PIN_MPP_MODE_INVALID 7
  152. #define QPNP_PIN_INVERT_INVALID 2
  153. #define QPNP_PIN_OUT_BUF_INVALID 3
  154. #define QPNP_PIN_GPIO_LV_MV_OUT_BUF_INVALID 4
  155. #define QPNP_PIN_VIN_4CH_INVALID 5
  156. #define QPNP_PIN_VIN_8CH_INVALID 8
  157. #define QPNP_PIN_GPIO_LV_VIN_INVALID 1
  158. #define QPNP_PIN_GPIO_MV_VIN_INVALID 2
  159. #define QPNP_PIN_GPIO_PULL_INVALID 6
  160. #define QPNP_PIN_MPP_PULL_INVALID 4
  161. #define QPNP_PIN_OUT_STRENGTH_INVALID 4
  162. #define QPNP_PIN_SRC_INVALID 8
  163. #define QPNP_PIN_GPIO_LV_MV_SRC_INVALID 16
  164. #define QPNP_PIN_MASTER_INVALID 2
  165. #define QPNP_PIN_AOUT_REF_INVALID 8
  166. #define QPNP_PIN_AIN_ROUTE_INVALID 8
  167. #define QPNP_PIN_CS_OUT_INVALID 8
  168. #define QPNP_PIN_APASS_SEL_INVALID 4
  169. #define QPNP_PIN_DTEST_SEL_INVALID 4
  170. struct qpnp_pin_spec {
  171. uint8_t slave; /* 0-15 */
  172. uint16_t offset; /* 0-255 */
  173. uint32_t gpio_chip_idx; /* offset from gpio_chip base */
  174. uint32_t pmic_pin; /* PMIC pin number */
  175. int irq; /* logical IRQ number */
  176. u8 regs[Q_NUM_CTL_REGS]; /* Control regs */
  177. u8 num_ctl_regs; /* usable number on this pin */
  178. u8 type; /* peripheral type */
  179. u8 subtype; /* peripheral subtype */
  180. u8 dig_major_rev;
  181. struct device_node *node;
  182. enum qpnp_pin_param_type params[Q_NUM_PARAMS];
  183. struct qpnp_pin_chip *q_chip;
  184. };
  185. struct qpnp_pin_chip {
  186. struct gpio_chip gpio_chip;
  187. struct platform_device *pdev;
  188. struct regmap *regmap;
  189. struct qpnp_pin_spec **pmic_pins;
  190. struct qpnp_pin_spec **chip_gpios;
  191. uint32_t pmic_pin_lowest;
  192. uint32_t pmic_pin_highest;
  193. struct device_node *int_ctrl;
  194. struct list_head chip_list;
  195. struct dentry *dfs_dir;
  196. bool chip_registered;
  197. };
  198. static LIST_HEAD(qpnp_pin_chips);
  199. static DEFINE_MUTEX(qpnp_pin_chips_lock);
  200. static inline void qpnp_pmic_pin_set_spec(struct qpnp_pin_chip *q_chip,
  201. uint32_t pmic_pin,
  202. struct qpnp_pin_spec *spec)
  203. {
  204. q_chip->pmic_pins[pmic_pin - q_chip->pmic_pin_lowest] = spec;
  205. }
  206. static inline struct qpnp_pin_spec *qpnp_pmic_pin_get_spec(
  207. struct qpnp_pin_chip *q_chip,
  208. uint32_t pmic_pin)
  209. {
  210. if (pmic_pin < q_chip->pmic_pin_lowest ||
  211. pmic_pin > q_chip->pmic_pin_highest)
  212. return NULL;
  213. return q_chip->pmic_pins[pmic_pin - q_chip->pmic_pin_lowest];
  214. }
  215. static inline struct qpnp_pin_spec *qpnp_chip_gpio_get_spec(
  216. struct qpnp_pin_chip *q_chip,
  217. uint32_t chip_gpio)
  218. {
  219. if (chip_gpio >= q_chip->gpio_chip.ngpio)
  220. return NULL;
  221. return q_chip->chip_gpios[chip_gpio];
  222. }
  223. static inline void qpnp_chip_gpio_set_spec(struct qpnp_pin_chip *q_chip,
  224. uint32_t chip_gpio,
  225. struct qpnp_pin_spec *spec)
  226. {
  227. q_chip->chip_gpios[chip_gpio] = spec;
  228. }
  229. static bool is_gpio_lv_mv(struct qpnp_pin_spec *q_spec)
  230. {
  231. if ((q_spec->type == Q_GPIO_TYPE) &&
  232. (q_spec->subtype == Q_GPIO_SUBTYPE_GPIO_LV ||
  233. q_spec->subtype == Q_GPIO_SUBTYPE_GPIO_MV))
  234. return true;
  235. return false;
  236. }
  237. /*
  238. * Determines whether a specified param's configuration is correct.
  239. * This check is two tier. First a check is done whether the hardware
  240. * supports this param and value requested. The second check validates
  241. * that the configuration is correct, given the fact that the hardware
  242. * supports it.
  243. *
  244. * Returns
  245. * -ENXIO is the hardware does not support this param.
  246. * -EINVAL if the the hardware does support this param, but the
  247. * requested value is outside the supported range.
  248. */
  249. static int qpnp_pin_check_config(enum qpnp_pin_param_type idx,
  250. struct qpnp_pin_spec *q_spec, uint32_t val)
  251. {
  252. u8 subtype = q_spec->subtype;
  253. switch (idx) {
  254. case Q_PIN_CFG_MODE:
  255. if (q_spec->type == Q_GPIO_TYPE) {
  256. if (is_gpio_lv_mv(q_spec)) {
  257. if (val >= QPNP_PIN_GPIO_LV_MV_MODE_INVALID)
  258. return -EINVAL;
  259. } else if (val >= QPNP_PIN_GPIO_MODE_INVALID) {
  260. return -EINVAL;
  261. }
  262. } else if (q_spec->type == Q_MPP_TYPE) {
  263. if (val >= QPNP_PIN_MPP_MODE_INVALID)
  264. return -EINVAL;
  265. if ((subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT ||
  266. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_SINK) &&
  267. (val == QPNP_PIN_MODE_BIDIR))
  268. return -ENXIO;
  269. }
  270. break;
  271. case Q_PIN_CFG_OUTPUT_TYPE:
  272. if (q_spec->type != Q_GPIO_TYPE)
  273. return -ENXIO;
  274. if ((val == QPNP_PIN_OUT_BUF_OPEN_DRAIN_NMOS ||
  275. val == QPNP_PIN_OUT_BUF_OPEN_DRAIN_PMOS) &&
  276. (subtype == Q_GPIO_SUBTYPE_GPIOC_4CH ||
  277. (subtype == Q_GPIO_SUBTYPE_GPIOC_8CH)))
  278. return -EINVAL;
  279. else if (is_gpio_lv_mv(q_spec) &&
  280. val >= QPNP_PIN_GPIO_LV_MV_OUT_BUF_INVALID)
  281. return -EINVAL;
  282. else if (val >= QPNP_PIN_OUT_BUF_INVALID)
  283. return -EINVAL;
  284. break;
  285. case Q_PIN_CFG_INVERT:
  286. if (val >= QPNP_PIN_INVERT_INVALID)
  287. return -EINVAL;
  288. break;
  289. case Q_PIN_CFG_PULL:
  290. if (q_spec->type == Q_GPIO_TYPE &&
  291. val >= QPNP_PIN_GPIO_PULL_INVALID)
  292. return -EINVAL;
  293. if (q_spec->type == Q_MPP_TYPE) {
  294. if (val >= QPNP_PIN_MPP_PULL_INVALID)
  295. return -EINVAL;
  296. if (subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT ||
  297. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_SINK)
  298. return -ENXIO;
  299. }
  300. break;
  301. case Q_PIN_CFG_VIN_SEL:
  302. if (is_gpio_lv_mv(q_spec)) {
  303. if (subtype == Q_GPIO_SUBTYPE_GPIO_LV) {
  304. if (val >= QPNP_PIN_GPIO_LV_VIN_INVALID)
  305. return -EINVAL;
  306. } else {
  307. if (val >= QPNP_PIN_GPIO_MV_VIN_INVALID)
  308. return -EINVAL;
  309. }
  310. } else if (val >= QPNP_PIN_VIN_8CH_INVALID) {
  311. return -EINVAL;
  312. } else if (val >= QPNP_PIN_VIN_4CH_INVALID) {
  313. if (q_spec->type == Q_GPIO_TYPE &&
  314. (subtype == Q_GPIO_SUBTYPE_GPIO_4CH ||
  315. subtype == Q_GPIO_SUBTYPE_GPIOC_4CH))
  316. return -EINVAL;
  317. if (q_spec->type == Q_MPP_TYPE &&
  318. (subtype == Q_MPP_SUBTYPE_4CH_NO_ANA_OUT ||
  319. subtype == Q_MPP_SUBTYPE_4CH_NO_SINK ||
  320. subtype == Q_MPP_SUBTYPE_4CH_FULL_FUNC ||
  321. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT ||
  322. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_SINK))
  323. return -EINVAL;
  324. }
  325. break;
  326. case Q_PIN_CFG_OUT_STRENGTH:
  327. if (q_spec->type != Q_GPIO_TYPE)
  328. return -ENXIO;
  329. if (val >= QPNP_PIN_OUT_STRENGTH_INVALID ||
  330. val == 0)
  331. return -EINVAL;
  332. break;
  333. case Q_PIN_CFG_SRC_SEL:
  334. if (q_spec->type == Q_MPP_TYPE &&
  335. (val == QPNP_PIN_SEL_FUNC_1 ||
  336. val == QPNP_PIN_SEL_FUNC_2))
  337. return -EINVAL;
  338. if (is_gpio_lv_mv(q_spec)) {
  339. if (val >= QPNP_PIN_GPIO_LV_MV_SRC_INVALID)
  340. return -EINVAL;
  341. } else if (val >= QPNP_PIN_SRC_INVALID) {
  342. return -EINVAL;
  343. }
  344. break;
  345. case Q_PIN_CFG_MASTER_EN:
  346. if (val >= QPNP_PIN_MASTER_INVALID)
  347. return -EINVAL;
  348. break;
  349. case Q_PIN_CFG_AOUT_REF:
  350. if (q_spec->type != Q_MPP_TYPE)
  351. return -ENXIO;
  352. if (subtype == Q_MPP_SUBTYPE_4CH_NO_ANA_OUT ||
  353. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT)
  354. return -ENXIO;
  355. if (val >= QPNP_PIN_AOUT_REF_INVALID)
  356. return -EINVAL;
  357. break;
  358. case Q_PIN_CFG_AIN_ROUTE:
  359. if (q_spec->type != Q_MPP_TYPE)
  360. return -ENXIO;
  361. if (val >= QPNP_PIN_AIN_ROUTE_INVALID)
  362. return -EINVAL;
  363. break;
  364. case Q_PIN_CFG_CS_OUT:
  365. if (q_spec->type != Q_MPP_TYPE)
  366. return -ENXIO;
  367. if (subtype == Q_MPP_SUBTYPE_4CH_NO_SINK ||
  368. subtype == Q_MPP_SUBTYPE_ULT_4CH_NO_SINK)
  369. return -ENXIO;
  370. if (val >= QPNP_PIN_CS_OUT_INVALID)
  371. return -EINVAL;
  372. break;
  373. case Q_PIN_CFG_APASS_SEL:
  374. if (!is_gpio_lv_mv(q_spec))
  375. return -ENXIO;
  376. if (val >= QPNP_PIN_APASS_SEL_INVALID)
  377. return -EINVAL;
  378. break;
  379. case Q_PIN_CFG_DTEST_SEL:
  380. if (val > QPNP_PIN_DTEST_SEL_INVALID)
  381. return -EINVAL;
  382. break;
  383. default:
  384. pr_err("invalid param type %u specified\n", idx);
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. #define Q_CHK_INVALID(idx, q_spec, val) \
  390. (qpnp_pin_check_config(idx, q_spec, val) == -EINVAL)
  391. static int qpnp_pin_check_constraints(struct qpnp_pin_spec *q_spec,
  392. struct qpnp_pin_cfg *param)
  393. {
  394. int pin = q_spec->pmic_pin;
  395. const char *name;
  396. name = (q_spec->type == Q_GPIO_TYPE) ? "gpio" : "mpp";
  397. if (Q_CHK_INVALID(Q_PIN_CFG_MODE, q_spec, param->mode))
  398. pr_err("invalid direction value %d for %s %d\n",
  399. param->mode, name, pin);
  400. else if (Q_CHK_INVALID(Q_PIN_CFG_INVERT, q_spec, param->invert))
  401. pr_err("invalid invert polarity value %d for %s %d\n",
  402. param->invert, name, pin);
  403. else if (Q_CHK_INVALID(Q_PIN_CFG_SRC_SEL, q_spec, param->src_sel))
  404. pr_err("invalid source select value %d for %s %d\n",
  405. param->src_sel, name, pin);
  406. else if (Q_CHK_INVALID(Q_PIN_CFG_OUT_STRENGTH,
  407. q_spec, param->out_strength))
  408. pr_err("invalid out strength value %d for %s %d\n",
  409. param->out_strength, name, pin);
  410. else if (Q_CHK_INVALID(Q_PIN_CFG_OUTPUT_TYPE,
  411. q_spec, param->output_type))
  412. pr_err("invalid out type value %d for %s %d\n",
  413. param->output_type, name, pin);
  414. else if (Q_CHK_INVALID(Q_PIN_CFG_VIN_SEL, q_spec, param->vin_sel))
  415. pr_err("invalid vin select %d value for %s %d\n",
  416. param->vin_sel, name, pin);
  417. else if (Q_CHK_INVALID(Q_PIN_CFG_PULL, q_spec, param->pull))
  418. pr_err("invalid pull value %d for pin %s %d\n",
  419. param->pull, name, pin);
  420. else if (Q_CHK_INVALID(Q_PIN_CFG_MASTER_EN, q_spec, param->master_en))
  421. pr_err("invalid master_en value %d for %s %d\n",
  422. param->master_en, name, pin);
  423. else if (Q_CHK_INVALID(Q_PIN_CFG_AOUT_REF, q_spec, param->aout_ref))
  424. pr_err("invalid aout_reg value %d for %s %d\n",
  425. param->aout_ref, name, pin);
  426. else if (Q_CHK_INVALID(Q_PIN_CFG_AIN_ROUTE, q_spec, param->ain_route))
  427. pr_err("invalid ain_route value %d for %s %d\n",
  428. param->ain_route, name, pin);
  429. else if (Q_CHK_INVALID(Q_PIN_CFG_CS_OUT, q_spec, param->cs_out))
  430. pr_err("invalid cs_out value %d for %s %d\n",
  431. param->cs_out, name, pin);
  432. else if (Q_CHK_INVALID(Q_PIN_CFG_APASS_SEL, q_spec, param->apass_sel))
  433. pr_err("invalid apass_sel value %d for %s %d\n",
  434. param->apass_sel, name, pin);
  435. else if (Q_CHK_INVALID(Q_PIN_CFG_DTEST_SEL, q_spec, param->dtest_sel))
  436. pr_err("invalid dtest_sel value %d for %s %d\n",
  437. param->dtest_sel, name, pin);
  438. else
  439. return 0;
  440. return -EINVAL;
  441. }
  442. static inline u8 q_reg_get(u8 *reg, int shift, int mask)
  443. {
  444. return (*reg & mask) >> shift;
  445. }
  446. static inline void q_reg_set(u8 *reg, int shift, int mask, int value)
  447. {
  448. *reg |= (value << shift) & mask;
  449. }
  450. static inline void q_reg_clr_set(u8 *reg, int shift, int mask, int value)
  451. {
  452. *reg &= ~mask;
  453. *reg |= (value << shift) & mask;
  454. }
  455. /*
  456. * Calculate the minimum number of registers that must be read / written
  457. * in order to satisfy the full feature set of the given pin.
  458. */
  459. static int qpnp_pin_ctl_regs_init(struct qpnp_pin_spec *q_spec)
  460. {
  461. if (q_spec->type == Q_GPIO_TYPE) {
  462. if (is_gpio_lv_mv(q_spec))
  463. q_spec->num_ctl_regs = 11;
  464. else
  465. q_spec->num_ctl_regs = 7;
  466. } else if (q_spec->type == Q_MPP_TYPE) {
  467. switch (q_spec->subtype) {
  468. case Q_MPP_SUBTYPE_4CH_NO_SINK:
  469. case Q_MPP_SUBTYPE_ULT_4CH_NO_SINK:
  470. q_spec->num_ctl_regs = 12;
  471. break;
  472. case Q_MPP_SUBTYPE_4CH_NO_ANA_OUT:
  473. case Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT:
  474. case Q_MPP_SUBTYPE_4CH_FULL_FUNC:
  475. case Q_MPP_SUBTYPE_8CH_FULL_FUNC:
  476. q_spec->num_ctl_regs = 13;
  477. break;
  478. default:
  479. pr_err("Invalid MPP subtype 0x%x\n", q_spec->subtype);
  480. return -EINVAL;
  481. }
  482. } else {
  483. pr_err("Invalid type 0x%x\n", q_spec->type);
  484. return -EINVAL;
  485. }
  486. return 0;
  487. }
  488. static int qpnp_pin_read_regs(struct qpnp_pin_chip *q_chip,
  489. struct qpnp_pin_spec *q_spec)
  490. {
  491. int bytes_left = q_spec->num_ctl_regs;
  492. int rc;
  493. char *buf_p = &q_spec->regs[0];
  494. u16 reg_addr = Q_REG_ADDR(q_spec, Q_REG_MODE_CTL);
  495. while (bytes_left > 0) {
  496. rc = regmap_bulk_read(q_chip->regmap, reg_addr, buf_p,
  497. bytes_left < 8 ? bytes_left : 8);
  498. if (rc)
  499. return rc;
  500. bytes_left -= 8;
  501. buf_p += 8;
  502. reg_addr += 8;
  503. }
  504. return 0;
  505. }
  506. static int qpnp_pin_write_regs(struct qpnp_pin_chip *q_chip,
  507. struct qpnp_pin_spec *q_spec)
  508. {
  509. int bytes_left = q_spec->num_ctl_regs;
  510. int rc;
  511. char *buf_p = &q_spec->regs[0];
  512. u16 reg_addr = Q_REG_ADDR(q_spec, Q_REG_MODE_CTL);
  513. while (bytes_left > 0) {
  514. rc = regmap_bulk_write(q_chip->regmap, reg_addr, buf_p,
  515. bytes_left < 8 ? bytes_left : 8);
  516. if (rc)
  517. return rc;
  518. bytes_left -= 8;
  519. buf_p += 8;
  520. reg_addr += 8;
  521. }
  522. return 0;
  523. }
  524. static int qpnp_pin_cache_regs(struct qpnp_pin_chip *q_chip,
  525. struct qpnp_pin_spec *q_spec)
  526. {
  527. int rc;
  528. struct device *dev = &q_chip->pdev->dev;
  529. rc = qpnp_pin_read_regs(q_chip, q_spec);
  530. if (rc)
  531. dev_err(dev, "%s: unable to read control regs\n", __func__);
  532. return rc;
  533. }
  534. #define Q_HAVE_HW_SP(idx, q_spec, val) \
  535. (qpnp_pin_check_config(idx, q_spec, val) == 0)
  536. static int _qpnp_pin_config(struct qpnp_pin_chip *q_chip,
  537. struct qpnp_pin_spec *q_spec,
  538. struct qpnp_pin_cfg *param)
  539. {
  540. struct device *dev = &q_chip->pdev->dev;
  541. int rc;
  542. u8 shift, mask, *reg;
  543. rc = qpnp_pin_check_constraints(q_spec, param);
  544. if (rc)
  545. goto gpio_cfg;
  546. /* set mode */
  547. if (Q_HAVE_HW_SP(Q_PIN_CFG_MODE, q_spec, param->mode)) {
  548. if (is_gpio_lv_mv(q_spec)) {
  549. shift = Q_REG_LV_MV_MODE_SEL_SHIFT;
  550. mask = Q_REG_LV_MV_MODE_SEL_MASK;
  551. } else {
  552. shift = Q_REG_MODE_SEL_SHIFT;
  553. mask = Q_REG_MODE_SEL_MASK;
  554. }
  555. q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL],
  556. shift, mask, param->mode);
  557. }
  558. /* output specific configuration */
  559. if (Q_HAVE_HW_SP(Q_PIN_CFG_INVERT, q_spec, param->invert)) {
  560. if (is_gpio_lv_mv(q_spec)) {
  561. shift = Q_REG_DIG_OUT_SRC_INVERT_SHIFT;
  562. mask = Q_REG_DIG_OUT_SRC_INVERT_MASK;
  563. reg = &q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  564. } else {
  565. shift = Q_REG_OUT_INVERT_SHIFT;
  566. mask = Q_REG_OUT_INVERT_MASK;
  567. reg = &q_spec->regs[Q_REG_I_MODE_CTL];
  568. }
  569. q_reg_clr_set(reg, shift, mask, param->invert);
  570. }
  571. if (Q_HAVE_HW_SP(Q_PIN_CFG_SRC_SEL, q_spec, param->src_sel)) {
  572. if (is_gpio_lv_mv(q_spec)) {
  573. shift = Q_REG_DIG_OUT_SRC_SRC_SEL_SHIFT;
  574. mask = Q_REG_DIG_OUT_SRC_SRC_SEL_MASK;
  575. reg = &q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  576. } else {
  577. shift = Q_REG_SRC_SEL_SHIFT;
  578. mask = Q_REG_SRC_SEL_MASK;
  579. reg = &q_spec->regs[Q_REG_I_MODE_CTL];
  580. }
  581. q_reg_clr_set(reg, shift, mask, param->src_sel);
  582. }
  583. if (Q_HAVE_HW_SP(Q_PIN_CFG_OUT_STRENGTH, q_spec, param->out_strength))
  584. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_OUT_CTL],
  585. Q_REG_OUT_STRENGTH_SHIFT, Q_REG_OUT_STRENGTH_MASK,
  586. param->out_strength);
  587. if (Q_HAVE_HW_SP(Q_PIN_CFG_OUTPUT_TYPE, q_spec, param->output_type))
  588. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_OUT_CTL],
  589. Q_REG_OUT_TYPE_SHIFT, Q_REG_OUT_TYPE_MASK,
  590. param->output_type);
  591. /* input config */
  592. if (Q_HAVE_HW_SP(Q_PIN_CFG_DTEST_SEL, q_spec, param->dtest_sel)
  593. && param->dtest_sel) {
  594. if (is_gpio_lv_mv(q_spec)) {
  595. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_IN_CTL],
  596. Q_REG_LV_MV_DTEST_SEL_CFG_SHIFT,
  597. Q_REG_LV_MV_DTEST_SEL_CFG_MASK,
  598. param->dtest_sel - 1);
  599. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_IN_CTL],
  600. Q_REG_LV_MV_DTEST_SEL_EN_SHIFT,
  601. Q_REG_LV_MV_DTEST_SEL_EN_MASK, 0x1);
  602. } else {
  603. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_IN_CTL],
  604. Q_REG_DTEST_SEL_SHIFT,
  605. Q_REG_DTEST_SEL_MASK,
  606. BIT(param->dtest_sel - 1));
  607. }
  608. }
  609. /* config applicable for both input / output */
  610. if (Q_HAVE_HW_SP(Q_PIN_CFG_VIN_SEL, q_spec, param->vin_sel))
  611. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_VIN_CTL],
  612. Q_REG_VIN_SHIFT, Q_REG_VIN_MASK,
  613. param->vin_sel);
  614. if (Q_HAVE_HW_SP(Q_PIN_CFG_PULL, q_spec, param->pull))
  615. q_reg_clr_set(&q_spec->regs[Q_REG_I_DIG_PULL_CTL],
  616. Q_REG_PULL_SHIFT, Q_REG_PULL_MASK,
  617. param->pull);
  618. if (Q_HAVE_HW_SP(Q_PIN_CFG_MASTER_EN, q_spec, param->master_en))
  619. q_reg_clr_set(&q_spec->regs[Q_REG_I_EN_CTL],
  620. Q_REG_MASTER_EN_SHIFT, Q_REG_MASTER_EN_MASK,
  621. param->master_en);
  622. /* mpp specific config */
  623. if (Q_HAVE_HW_SP(Q_PIN_CFG_AOUT_REF, q_spec, param->aout_ref))
  624. q_reg_clr_set(&q_spec->regs[Q_REG_I_AOUT_CTL],
  625. Q_REG_AOUT_REF_SHIFT, Q_REG_AOUT_REF_MASK,
  626. param->aout_ref);
  627. if (Q_HAVE_HW_SP(Q_PIN_CFG_AIN_ROUTE, q_spec, param->ain_route))
  628. q_reg_clr_set(&q_spec->regs[Q_REG_I_AIN_CTL],
  629. Q_REG_AIN_ROUTE_SHIFT, Q_REG_AIN_ROUTE_MASK,
  630. param->ain_route);
  631. if (Q_HAVE_HW_SP(Q_PIN_CFG_CS_OUT, q_spec, param->cs_out))
  632. q_reg_clr_set(&q_spec->regs[Q_REG_I_SINK_CTL],
  633. Q_REG_CS_OUT_SHIFT, Q_REG_CS_OUT_MASK,
  634. param->cs_out);
  635. if (Q_HAVE_HW_SP(Q_PIN_CFG_APASS_SEL, q_spec, param->apass_sel))
  636. q_reg_clr_set(&q_spec->regs[Q_REG_I_APASS_SEL_CTL],
  637. Q_REG_APASS_SEL_SHIFT, Q_REG_APASS_SEL_MASK,
  638. param->apass_sel);
  639. rc = qpnp_pin_write_regs(q_chip, q_spec);
  640. if (rc) {
  641. dev_err(&q_chip->pdev->dev,
  642. "%s: unable to write master enable\n",
  643. __func__);
  644. goto gpio_cfg;
  645. }
  646. return 0;
  647. gpio_cfg:
  648. dev_err(dev, "%s: unable to set default config for pmic pin %d\n",
  649. __func__, q_spec->pmic_pin);
  650. return rc;
  651. }
  652. int qpnp_pin_config(int gpio, struct qpnp_pin_cfg *param)
  653. {
  654. int rc, chip_offset;
  655. struct qpnp_pin_chip *q_chip;
  656. struct qpnp_pin_spec *q_spec = NULL;
  657. struct gpio_chip *gpio_chip;
  658. if (param == NULL)
  659. return -EINVAL;
  660. mutex_lock(&qpnp_pin_chips_lock);
  661. list_for_each_entry(q_chip, &qpnp_pin_chips, chip_list) {
  662. gpio_chip = &q_chip->gpio_chip;
  663. if (gpio >= gpio_chip->base
  664. && gpio < gpio_chip->base + gpio_chip->ngpio) {
  665. chip_offset = gpio - gpio_chip->base;
  666. q_spec = qpnp_chip_gpio_get_spec(q_chip, chip_offset);
  667. if (WARN_ON(!q_spec)) {
  668. mutex_unlock(&qpnp_pin_chips_lock);
  669. return -ENODEV;
  670. }
  671. break;
  672. }
  673. }
  674. mutex_unlock(&qpnp_pin_chips_lock);
  675. if (!q_spec)
  676. return -ENODEV;
  677. rc = _qpnp_pin_config(q_chip, q_spec, param);
  678. return rc;
  679. }
  680. EXPORT_SYMBOL(qpnp_pin_config);
  681. int qpnp_pin_map(const char *name, uint32_t pmic_pin)
  682. {
  683. struct qpnp_pin_chip *q_chip;
  684. struct qpnp_pin_spec *q_spec = NULL;
  685. if (!name)
  686. return -EINVAL;
  687. mutex_lock(&qpnp_pin_chips_lock);
  688. list_for_each_entry(q_chip, &qpnp_pin_chips, chip_list) {
  689. if (strcmp(q_chip->gpio_chip.label, name) != 0)
  690. continue;
  691. if (q_chip->pmic_pin_lowest <= pmic_pin &&
  692. q_chip->pmic_pin_highest >= pmic_pin) {
  693. q_spec = qpnp_pmic_pin_get_spec(q_chip, pmic_pin);
  694. mutex_unlock(&qpnp_pin_chips_lock);
  695. if (WARN_ON(!q_spec))
  696. return -ENODEV;
  697. return q_chip->gpio_chip.base + q_spec->gpio_chip_idx;
  698. }
  699. }
  700. mutex_unlock(&qpnp_pin_chips_lock);
  701. return -EINVAL;
  702. }
  703. EXPORT_SYMBOL(qpnp_pin_map);
  704. static int qpnp_pin_to_irq(struct gpio_chip *gpio_chip, unsigned int offset)
  705. {
  706. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  707. struct qpnp_pin_spec *q_spec;
  708. struct of_phandle_args oirq;
  709. q_spec = qpnp_chip_gpio_get_spec(q_chip, offset);
  710. if (!q_spec)
  711. return -EINVAL;
  712. /* if we have mapped this pin previously return the virq */
  713. if (q_spec->irq)
  714. return q_spec->irq;
  715. /* call into irq_domain to get irq mapping */
  716. oirq.np = q_chip->int_ctrl;
  717. oirq.args[0] = to_spmi_device(q_chip->pdev->dev.parent)->usid;
  718. oirq.args[1] = (q_spec->offset >> 8) & 0xFF;
  719. oirq.args[2] = 0;
  720. oirq.args[3] = IRQ_TYPE_NONE;
  721. oirq.args_count = 4;
  722. q_spec->irq = irq_create_of_mapping(&oirq);
  723. if (!q_spec->irq) {
  724. dev_err(&q_chip->pdev->dev, "%s: invalid irq for gpio %u\n",
  725. __func__, q_spec->pmic_pin);
  726. WARN_ON(1);
  727. return -EINVAL;
  728. }
  729. return q_spec->irq;
  730. }
  731. static int qpnp_pin_get(struct gpio_chip *gpio_chip, unsigned int offset)
  732. {
  733. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  734. struct qpnp_pin_spec *q_spec = NULL;
  735. u8 buf, en_mask, shift, mask, reg;
  736. unsigned int val;
  737. int rc;
  738. if (WARN_ON(!q_chip))
  739. return -ENODEV;
  740. q_spec = qpnp_chip_gpio_get_spec(q_chip, offset);
  741. if (WARN_ON(!q_spec))
  742. return -ENODEV;
  743. if (is_gpio_lv_mv(q_spec)) {
  744. mask = Q_REG_LV_MV_MODE_SEL_MASK;
  745. shift = Q_REG_LV_MV_MODE_SEL_SHIFT;
  746. } else {
  747. mask = Q_REG_MODE_SEL_MASK;
  748. shift = Q_REG_MODE_SEL_SHIFT;
  749. }
  750. /* gpio val is from RT status iff input is enabled */
  751. if (q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL], shift, mask)
  752. == QPNP_PIN_MODE_DIG_IN) {
  753. rc = regmap_read(q_chip->regmap,
  754. Q_REG_ADDR(q_spec, Q_REG_STATUS1), &val);
  755. if (rc)
  756. return rc;
  757. buf = val;
  758. if (q_spec->type == Q_GPIO_TYPE && q_spec->dig_major_rev == 0)
  759. en_mask = Q_REG_STATUS1_GPIO_EN_REV0_MASK;
  760. else if (q_spec->type == Q_GPIO_TYPE &&
  761. q_spec->dig_major_rev > 0)
  762. en_mask = Q_REG_STATUS1_GPIO_EN_MASK;
  763. else /* MPP */
  764. en_mask = Q_REG_STATUS1_MPP_EN_MASK;
  765. if (!(buf & en_mask))
  766. return -EPERM;
  767. return buf & Q_REG_STATUS1_VAL_MASK;
  768. }
  769. if (is_gpio_lv_mv(q_spec)) {
  770. shift = Q_REG_DIG_OUT_SRC_INVERT_SHIFT;
  771. mask = Q_REG_DIG_OUT_SRC_INVERT_MASK;
  772. reg = q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  773. } else {
  774. shift = Q_REG_OUT_INVERT_SHIFT;
  775. mask = Q_REG_OUT_INVERT_MASK;
  776. reg = q_spec->regs[Q_REG_I_MODE_CTL];
  777. }
  778. return (reg & mask) >> shift;
  779. }
  780. static int __qpnp_pin_set(struct qpnp_pin_chip *q_chip,
  781. struct qpnp_pin_spec *q_spec, int value)
  782. {
  783. int rc;
  784. u8 shift, mask, *reg;
  785. u16 address;
  786. if (!q_chip || !q_spec)
  787. return -EINVAL;
  788. if (is_gpio_lv_mv(q_spec)) {
  789. shift = Q_REG_DIG_OUT_SRC_INVERT_SHIFT;
  790. mask = Q_REG_DIG_OUT_SRC_INVERT_MASK;
  791. reg = &q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  792. address = Q_REG_ADDR(q_spec, Q_REG_DIG_OUT_SRC_CTL);
  793. } else {
  794. shift = Q_REG_OUT_INVERT_SHIFT;
  795. mask = Q_REG_OUT_INVERT_MASK;
  796. reg = &q_spec->regs[Q_REG_I_MODE_CTL];
  797. address = Q_REG_ADDR(q_spec, Q_REG_MODE_CTL);
  798. }
  799. q_reg_clr_set(reg, shift, mask, !!value);
  800. rc = regmap_write(q_chip->regmap, address, *reg);
  801. if (rc)
  802. dev_err(&q_chip->pdev->dev, "%s: spmi write failed\n",
  803. __func__);
  804. return rc;
  805. }
  806. static void qpnp_pin_set(struct gpio_chip *gpio_chip,
  807. unsigned int offset, int value)
  808. {
  809. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  810. struct qpnp_pin_spec *q_spec;
  811. if (WARN_ON(!q_chip))
  812. return;
  813. q_spec = qpnp_chip_gpio_get_spec(q_chip, offset);
  814. if (WARN_ON(!q_spec))
  815. return;
  816. __qpnp_pin_set(q_chip, q_spec, value);
  817. }
  818. static int qpnp_pin_set_mode(struct qpnp_pin_chip *q_chip,
  819. struct qpnp_pin_spec *q_spec, int mode)
  820. {
  821. int rc;
  822. u8 shift, mask;
  823. if (!q_chip || !q_spec)
  824. return -EINVAL;
  825. if (qpnp_pin_check_config(Q_PIN_CFG_MODE, q_spec, mode)) {
  826. pr_err("invalid mode specification %d\n", mode);
  827. return -EINVAL;
  828. }
  829. if (is_gpio_lv_mv(q_spec)) {
  830. shift = Q_REG_LV_MV_MODE_SEL_SHIFT;
  831. mask = Q_REG_LV_MV_MODE_SEL_MASK;
  832. } else {
  833. shift = Q_REG_MODE_SEL_SHIFT;
  834. mask = Q_REG_MODE_SEL_MASK;
  835. }
  836. q_reg_clr_set(&q_spec->regs[Q_REG_I_MODE_CTL], shift, mask, mode);
  837. rc = regmap_write(q_chip->regmap, Q_REG_ADDR(q_spec, Q_REG_MODE_CTL),
  838. *&q_spec->regs[Q_REG_I_MODE_CTL]);
  839. return rc;
  840. }
  841. static int qpnp_pin_direction_input(struct gpio_chip *gpio_chip,
  842. unsigned int offset)
  843. {
  844. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  845. struct qpnp_pin_spec *q_spec;
  846. if (WARN_ON(!q_chip))
  847. return -ENODEV;
  848. q_spec = qpnp_chip_gpio_get_spec(q_chip, offset);
  849. if (WARN_ON(!q_spec))
  850. return -ENODEV;
  851. return qpnp_pin_set_mode(q_chip, q_spec, QPNP_PIN_MODE_DIG_IN);
  852. }
  853. static int qpnp_pin_direction_output(struct gpio_chip *gpio_chip,
  854. unsigned int offset, int val)
  855. {
  856. int rc;
  857. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  858. struct qpnp_pin_spec *q_spec;
  859. if (WARN_ON(!q_chip))
  860. return -ENODEV;
  861. q_spec = qpnp_chip_gpio_get_spec(q_chip, offset);
  862. if (WARN_ON(!q_spec))
  863. return -ENODEV;
  864. rc = __qpnp_pin_set(q_chip, q_spec, val);
  865. if (rc)
  866. return rc;
  867. rc = qpnp_pin_set_mode(q_chip, q_spec, QPNP_PIN_MODE_DIG_OUT);
  868. return rc;
  869. }
  870. static int qpnp_pin_of_gpio_xlate(struct gpio_chip *gpio_chip,
  871. const struct of_phandle_args *gpio_spec,
  872. u32 *flags)
  873. {
  874. struct qpnp_pin_chip *q_chip = gpiochip_get_data(gpio_chip);
  875. struct qpnp_pin_spec *q_spec;
  876. if (WARN_ON(gpio_chip->of_gpio_n_cells < 2)) {
  877. pr_err("of_gpio_n_cells < 2\n");
  878. return -EINVAL;
  879. }
  880. q_spec = qpnp_pmic_pin_get_spec(q_chip, gpio_spec->args[0]);
  881. if (!q_spec) {
  882. pr_err("no such PMIC gpio %u in device topology\n",
  883. gpio_spec->args[0]);
  884. return -EINVAL;
  885. }
  886. if (flags)
  887. *flags = gpio_spec->args[1];
  888. return q_spec->gpio_chip_idx;
  889. }
  890. static int qpnp_pin_apply_config(struct qpnp_pin_chip *q_chip,
  891. struct qpnp_pin_spec *q_spec)
  892. {
  893. struct qpnp_pin_cfg param;
  894. struct device_node *node = q_spec->node;
  895. int rc;
  896. u8 shift, mask, *reg;
  897. if (is_gpio_lv_mv(q_spec)) {
  898. shift = Q_REG_LV_MV_MODE_SEL_SHIFT;
  899. mask = Q_REG_LV_MV_MODE_SEL_MASK;
  900. } else {
  901. shift = Q_REG_MODE_SEL_SHIFT;
  902. mask = Q_REG_MODE_SEL_MASK;
  903. }
  904. param.mode = q_reg_get(&q_spec->regs[Q_REG_I_MODE_CTL],
  905. shift, mask);
  906. param.output_type = q_reg_get(&q_spec->regs[Q_REG_I_DIG_OUT_CTL],
  907. Q_REG_OUT_TYPE_SHIFT,
  908. Q_REG_OUT_TYPE_MASK);
  909. if (is_gpio_lv_mv(q_spec)) {
  910. shift = Q_REG_DIG_OUT_SRC_INVERT_SHIFT;
  911. mask = Q_REG_DIG_OUT_SRC_INVERT_MASK;
  912. reg = &q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  913. } else {
  914. shift = Q_REG_OUT_INVERT_SHIFT;
  915. mask = Q_REG_OUT_INVERT_MASK;
  916. reg = &q_spec->regs[Q_REG_I_MODE_CTL];
  917. }
  918. param.invert = q_reg_get(reg, shift, mask);
  919. param.pull = q_reg_get(&q_spec->regs[Q_REG_I_DIG_PULL_CTL],
  920. Q_REG_PULL_SHIFT, Q_REG_PULL_MASK);
  921. param.vin_sel = q_reg_get(&q_spec->regs[Q_REG_I_DIG_VIN_CTL],
  922. Q_REG_VIN_SHIFT, Q_REG_VIN_MASK);
  923. param.out_strength = q_reg_get(&q_spec->regs[Q_REG_I_DIG_OUT_CTL],
  924. Q_REG_OUT_STRENGTH_SHIFT,
  925. Q_REG_OUT_STRENGTH_MASK);
  926. if (is_gpio_lv_mv(q_spec)) {
  927. shift = Q_REG_DIG_OUT_SRC_SRC_SEL_SHIFT;
  928. mask = Q_REG_DIG_OUT_SRC_SRC_SEL_MASK;
  929. reg = &q_spec->regs[Q_REG_I_DIG_OUT_SRC_CTL];
  930. } else {
  931. shift = Q_REG_SRC_SEL_SHIFT;
  932. mask = Q_REG_SRC_SEL_MASK;
  933. reg = &q_spec->regs[Q_REG_I_MODE_CTL];
  934. }
  935. param.src_sel = q_reg_get(reg, shift, mask);
  936. param.master_en = q_reg_get(&q_spec->regs[Q_REG_I_EN_CTL],
  937. Q_REG_MASTER_EN_SHIFT,
  938. Q_REG_MASTER_EN_MASK);
  939. param.aout_ref = q_reg_get(&q_spec->regs[Q_REG_I_AOUT_CTL],
  940. Q_REG_AOUT_REF_SHIFT,
  941. Q_REG_AOUT_REF_MASK);
  942. param.ain_route = q_reg_get(&q_spec->regs[Q_REG_I_AIN_CTL],
  943. Q_REG_AIN_ROUTE_SHIFT,
  944. Q_REG_AIN_ROUTE_MASK);
  945. param.cs_out = q_reg_get(&q_spec->regs[Q_REG_I_SINK_CTL],
  946. Q_REG_CS_OUT_SHIFT,
  947. Q_REG_CS_OUT_MASK);
  948. param.apass_sel = q_reg_get(&q_spec->regs[Q_REG_I_APASS_SEL_CTL],
  949. Q_REG_APASS_SEL_SHIFT,
  950. Q_REG_APASS_SEL_MASK);
  951. if (is_gpio_lv_mv(q_spec)) {
  952. param.dtest_sel = q_reg_get(&q_spec->regs[Q_REG_I_DIG_IN_CTL],
  953. Q_REG_LV_MV_DTEST_SEL_CFG_SHIFT,
  954. Q_REG_LV_MV_DTEST_SEL_CFG_MASK);
  955. } else {
  956. param.dtest_sel = q_reg_get(&q_spec->regs[Q_REG_I_DIG_IN_CTL],
  957. Q_REG_DTEST_SEL_SHIFT,
  958. Q_REG_DTEST_SEL_MASK);
  959. }
  960. of_property_read_u32(node, "qcom,mode",
  961. &param.mode);
  962. of_property_read_u32(node, "qcom,output-type",
  963. &param.output_type);
  964. of_property_read_u32(node, "qcom,invert",
  965. &param.invert);
  966. of_property_read_u32(node, "qcom,pull",
  967. &param.pull);
  968. of_property_read_u32(node, "qcom,vin-sel",
  969. &param.vin_sel);
  970. of_property_read_u32(node, "qcom,out-strength",
  971. &param.out_strength);
  972. of_property_read_u32(node, "qcom,src-sel",
  973. &param.src_sel);
  974. of_property_read_u32(node, "qcom,master-en",
  975. &param.master_en);
  976. of_property_read_u32(node, "qcom,aout-ref",
  977. &param.aout_ref);
  978. of_property_read_u32(node, "qcom,ain-route",
  979. &param.ain_route);
  980. of_property_read_u32(node, "qcom,cs-out",
  981. &param.cs_out);
  982. of_property_read_u32(node, "qcom,apass-sel",
  983. &param.apass_sel);
  984. of_property_read_u32(node, "qcom,dtest-sel",
  985. &param.dtest_sel);
  986. rc = _qpnp_pin_config(q_chip, q_spec, &param);
  987. return rc;
  988. }
  989. static int qpnp_pin_free_chip(struct qpnp_pin_chip *q_chip)
  990. {
  991. int i, rc = 0;
  992. if (q_chip->chip_gpios)
  993. for (i = 0; i < q_chip->gpio_chip.ngpio; i++)
  994. kfree(q_chip->chip_gpios[i]);
  995. mutex_lock(&qpnp_pin_chips_lock);
  996. list_del(&q_chip->chip_list);
  997. mutex_unlock(&qpnp_pin_chips_lock);
  998. if (q_chip->chip_registered)
  999. gpiochip_remove(&q_chip->gpio_chip);
  1000. kfree(q_chip->chip_gpios);
  1001. kfree(q_chip->pmic_pins);
  1002. kfree(q_chip);
  1003. return rc;
  1004. }
  1005. #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG
  1006. struct qpnp_pin_reg {
  1007. uint32_t addr;
  1008. uint32_t idx;
  1009. uint32_t shift;
  1010. uint32_t mask;
  1011. };
  1012. static struct dentry *driver_dfs_dir;
  1013. static int qpnp_pin_reg_attr(enum qpnp_pin_param_type type,
  1014. struct qpnp_pin_reg *cfg,
  1015. struct qpnp_pin_spec *q_spec)
  1016. {
  1017. switch (type) {
  1018. case Q_PIN_CFG_MODE:
  1019. if (is_gpio_lv_mv(q_spec)) {
  1020. cfg->shift = Q_REG_LV_MV_MODE_SEL_SHIFT;
  1021. cfg->mask = Q_REG_LV_MV_MODE_SEL_MASK;
  1022. } else {
  1023. cfg->shift = Q_REG_MODE_SEL_SHIFT;
  1024. cfg->mask = Q_REG_MODE_SEL_MASK;
  1025. }
  1026. cfg->addr = Q_REG_MODE_CTL;
  1027. cfg->idx = Q_REG_I_MODE_CTL;
  1028. break;
  1029. case Q_PIN_CFG_OUTPUT_TYPE:
  1030. cfg->addr = Q_REG_DIG_OUT_CTL;
  1031. cfg->idx = Q_REG_I_DIG_OUT_CTL;
  1032. cfg->shift = Q_REG_OUT_TYPE_SHIFT;
  1033. cfg->mask = Q_REG_OUT_TYPE_MASK;
  1034. break;
  1035. case Q_PIN_CFG_INVERT:
  1036. if (is_gpio_lv_mv(q_spec)) {
  1037. cfg->addr = Q_REG_DIG_OUT_SRC_CTL;
  1038. cfg->idx = Q_REG_I_DIG_OUT_SRC_CTL;
  1039. cfg->shift = Q_REG_DIG_OUT_SRC_INVERT_SHIFT;
  1040. cfg->mask = Q_REG_DIG_OUT_SRC_INVERT_MASK;
  1041. } else {
  1042. cfg->addr = Q_REG_MODE_CTL;
  1043. cfg->idx = Q_REG_I_MODE_CTL;
  1044. cfg->shift = Q_REG_OUT_INVERT_SHIFT;
  1045. cfg->mask = Q_REG_OUT_INVERT_MASK;
  1046. }
  1047. break;
  1048. case Q_PIN_CFG_PULL:
  1049. cfg->addr = Q_REG_DIG_PULL_CTL;
  1050. cfg->idx = Q_REG_I_DIG_PULL_CTL;
  1051. cfg->shift = Q_REG_PULL_SHIFT;
  1052. cfg->mask = Q_REG_PULL_MASK;
  1053. break;
  1054. case Q_PIN_CFG_VIN_SEL:
  1055. cfg->addr = Q_REG_DIG_VIN_CTL;
  1056. cfg->idx = Q_REG_I_DIG_VIN_CTL;
  1057. cfg->shift = Q_REG_VIN_SHIFT;
  1058. cfg->mask = Q_REG_VIN_MASK;
  1059. break;
  1060. case Q_PIN_CFG_OUT_STRENGTH:
  1061. cfg->addr = Q_REG_DIG_OUT_CTL;
  1062. cfg->idx = Q_REG_I_DIG_OUT_CTL;
  1063. cfg->shift = Q_REG_OUT_STRENGTH_SHIFT;
  1064. cfg->mask = Q_REG_OUT_STRENGTH_MASK;
  1065. break;
  1066. case Q_PIN_CFG_SRC_SEL:
  1067. if (is_gpio_lv_mv(q_spec)) {
  1068. cfg->addr = Q_REG_DIG_OUT_SRC_CTL;
  1069. cfg->idx = Q_REG_I_DIG_OUT_SRC_CTL;
  1070. cfg->shift = Q_REG_DIG_OUT_SRC_SRC_SEL_SHIFT;
  1071. cfg->mask = Q_REG_DIG_OUT_SRC_SRC_SEL_MASK;
  1072. } else {
  1073. cfg->addr = Q_REG_MODE_CTL;
  1074. cfg->idx = Q_REG_I_MODE_CTL;
  1075. cfg->shift = Q_REG_SRC_SEL_SHIFT;
  1076. cfg->mask = Q_REG_SRC_SEL_MASK;
  1077. }
  1078. break;
  1079. case Q_PIN_CFG_MASTER_EN:
  1080. cfg->addr = Q_REG_EN_CTL;
  1081. cfg->idx = Q_REG_I_EN_CTL;
  1082. cfg->shift = Q_REG_MASTER_EN_SHIFT;
  1083. cfg->mask = Q_REG_MASTER_EN_MASK;
  1084. break;
  1085. case Q_PIN_CFG_AOUT_REF:
  1086. cfg->addr = Q_REG_AOUT_CTL;
  1087. cfg->idx = Q_REG_I_AOUT_CTL;
  1088. cfg->shift = Q_REG_AOUT_REF_SHIFT;
  1089. cfg->mask = Q_REG_AOUT_REF_MASK;
  1090. break;
  1091. case Q_PIN_CFG_AIN_ROUTE:
  1092. cfg->addr = Q_REG_AIN_CTL;
  1093. cfg->idx = Q_REG_I_AIN_CTL;
  1094. cfg->shift = Q_REG_AIN_ROUTE_SHIFT;
  1095. cfg->mask = Q_REG_AIN_ROUTE_MASK;
  1096. break;
  1097. case Q_PIN_CFG_CS_OUT:
  1098. cfg->addr = Q_REG_SINK_CTL;
  1099. cfg->idx = Q_REG_I_SINK_CTL;
  1100. cfg->shift = Q_REG_CS_OUT_SHIFT;
  1101. cfg->mask = Q_REG_CS_OUT_MASK;
  1102. break;
  1103. case Q_PIN_CFG_APASS_SEL:
  1104. cfg->addr = Q_REG_APASS_SEL_CTL;
  1105. cfg->idx = Q_REG_I_APASS_SEL_CTL;
  1106. cfg->shift = Q_REG_APASS_SEL_SHIFT;
  1107. cfg->mask = Q_REG_APASS_SEL_MASK;
  1108. break;
  1109. case Q_PIN_CFG_DTEST_SEL:
  1110. if (is_gpio_lv_mv(q_spec)) {
  1111. cfg->shift = Q_REG_LV_MV_DTEST_SEL_CFG_SHIFT;
  1112. cfg->mask = Q_REG_LV_MV_DTEST_SEL_CFG_MASK;
  1113. } else {
  1114. cfg->shift = Q_REG_DTEST_SEL_SHIFT;
  1115. cfg->mask = Q_REG_DTEST_SEL_MASK;
  1116. }
  1117. cfg->addr = Q_REG_DIG_IN_CTL;
  1118. cfg->idx = Q_REG_I_DIG_IN_CTL;
  1119. break;
  1120. default:
  1121. return -EINVAL;
  1122. }
  1123. return 0;
  1124. }
  1125. static int qpnp_pin_debugfs_get(void *data, u64 *val)
  1126. {
  1127. enum qpnp_pin_param_type *idx = data;
  1128. struct qpnp_pin_spec *q_spec;
  1129. struct qpnp_pin_reg cfg = {};
  1130. int rc;
  1131. q_spec = container_of(idx, struct qpnp_pin_spec, params[*idx]);
  1132. rc = qpnp_pin_reg_attr(*idx, &cfg, q_spec);
  1133. if (rc)
  1134. return rc;
  1135. *val = q_reg_get(&q_spec->regs[cfg.idx], cfg.shift, cfg.mask);
  1136. return 0;
  1137. }
  1138. static int qpnp_pin_debugfs_set(void *data, u64 val)
  1139. {
  1140. enum qpnp_pin_param_type *idx = data;
  1141. struct qpnp_pin_spec *q_spec;
  1142. struct qpnp_pin_chip *q_chip;
  1143. struct qpnp_pin_reg cfg = {};
  1144. int rc;
  1145. q_spec = container_of(idx, struct qpnp_pin_spec, params[*idx]);
  1146. q_chip = q_spec->q_chip;
  1147. /*
  1148. * special handling for GPIO_LV/MV 'dtest-sel'
  1149. * if (dtest_sel == 0) then disable dtest-sel
  1150. * else enable and set dtest.
  1151. */
  1152. if ((q_spec->subtype == Q_GPIO_SUBTYPE_GPIO_LV ||
  1153. q_spec->subtype == Q_GPIO_SUBTYPE_GPIO_MV) &&
  1154. *idx == Q_PIN_CFG_DTEST_SEL) {
  1155. /* enable/disable DTEST */
  1156. cfg.shift = Q_REG_LV_MV_DTEST_SEL_EN_SHIFT;
  1157. cfg.mask = Q_REG_LV_MV_DTEST_SEL_EN_MASK;
  1158. cfg.addr = Q_REG_DIG_IN_CTL;
  1159. cfg.idx = Q_REG_I_DIG_IN_CTL;
  1160. q_reg_clr_set(&q_spec->regs[cfg.idx],
  1161. cfg.shift, cfg.mask, !!val);
  1162. }
  1163. rc = qpnp_pin_check_config(*idx, q_spec, val);
  1164. if (rc)
  1165. return rc;
  1166. rc = qpnp_pin_reg_attr(*idx, &cfg, q_spec);
  1167. if (rc)
  1168. return rc;
  1169. if (*idx == Q_PIN_CFG_DTEST_SEL && val) {
  1170. if (is_gpio_lv_mv(q_spec))
  1171. val -= 1;
  1172. else
  1173. val = BIT(val - 1);
  1174. }
  1175. q_reg_clr_set(&q_spec->regs[cfg.idx], cfg.shift, cfg.mask, val);
  1176. rc = regmap_write(q_chip->regmap, Q_REG_ADDR(q_spec, cfg.addr),
  1177. *&q_spec->regs[cfg.idx]);
  1178. return rc;
  1179. }
  1180. DEFINE_SIMPLE_ATTRIBUTE(qpnp_pin_fops, qpnp_pin_debugfs_get,
  1181. qpnp_pin_debugfs_set, "%llu\n");
  1182. #define DEBUGFS_BUF_SIZE 11 /* supports 2^32 in decimal */
  1183. struct qpnp_pin_debugfs_args {
  1184. enum qpnp_pin_param_type type;
  1185. const char *filename;
  1186. };
  1187. static struct qpnp_pin_debugfs_args dfs_args[Q_NUM_PARAMS] = {
  1188. { Q_PIN_CFG_MODE, "mode" },
  1189. { Q_PIN_CFG_OUTPUT_TYPE, "output_type" },
  1190. { Q_PIN_CFG_INVERT, "invert" },
  1191. { Q_PIN_CFG_PULL, "pull" },
  1192. { Q_PIN_CFG_VIN_SEL, "vin_sel" },
  1193. { Q_PIN_CFG_OUT_STRENGTH, "out_strength" },
  1194. { Q_PIN_CFG_SRC_SEL, "src_sel" },
  1195. { Q_PIN_CFG_MASTER_EN, "master_en" },
  1196. { Q_PIN_CFG_AOUT_REF, "aout_ref" },
  1197. { Q_PIN_CFG_AIN_ROUTE, "ain_route" },
  1198. { Q_PIN_CFG_CS_OUT, "cs_out" },
  1199. { Q_PIN_CFG_APASS_SEL, "apass_sel" },
  1200. { Q_PIN_CFG_DTEST_SEL, "dtest-sel" },
  1201. };
  1202. static int qpnp_pin_debugfs_create(struct qpnp_pin_chip *q_chip)
  1203. {
  1204. struct platform_device *pdev = q_chip->pdev;
  1205. struct device *dev = &pdev->dev;
  1206. struct qpnp_pin_spec *q_spec;
  1207. enum qpnp_pin_param_type *params;
  1208. enum qpnp_pin_param_type type;
  1209. char pmic_pin[DEBUGFS_BUF_SIZE];
  1210. const char *filename;
  1211. struct dentry *dfs, *dfs_io_dir;
  1212. int i, j, rc;
  1213. q_chip->dfs_dir = debugfs_create_dir(q_chip->gpio_chip.label,
  1214. driver_dfs_dir);
  1215. if (q_chip->dfs_dir == NULL) {
  1216. dev_err(dev, "%s: cannot register chip debugfs directory %s\n",
  1217. __func__, dev->of_node->name);
  1218. return -ENODEV;
  1219. }
  1220. for (i = 0; i < q_chip->gpio_chip.ngpio; i++) {
  1221. q_spec = qpnp_chip_gpio_get_spec(q_chip, i);
  1222. params = q_spec->params;
  1223. snprintf(pmic_pin, DEBUGFS_BUF_SIZE, "%u", q_spec->pmic_pin);
  1224. dfs_io_dir = debugfs_create_dir(pmic_pin, q_chip->dfs_dir);
  1225. if (dfs_io_dir == NULL)
  1226. goto dfs_err;
  1227. for (j = 0; j < Q_NUM_PARAMS; j++) {
  1228. type = dfs_args[j].type;
  1229. filename = dfs_args[j].filename;
  1230. /*
  1231. * Use a value of '0' to see if the pin has even basic
  1232. * support for a function. Do not create a file if
  1233. * it doesn't.
  1234. */
  1235. rc = qpnp_pin_check_config(type, q_spec, 0);
  1236. if (rc == -ENXIO)
  1237. continue;
  1238. params[type] = type;
  1239. dfs = debugfs_create_file(filename, 0644, dfs_io_dir,
  1240. &q_spec->params[type], &qpnp_pin_fops);
  1241. if (dfs == NULL)
  1242. goto dfs_err;
  1243. }
  1244. }
  1245. return 0;
  1246. dfs_err:
  1247. dev_err(dev, "%s: cannot register debugfs for pmic gpio %u on chip %s\n",
  1248. __func__, q_spec->pmic_pin, dev->of_node->name);
  1249. debugfs_remove_recursive(q_chip->dfs_dir);
  1250. return -ENFILE;
  1251. }
  1252. #else
  1253. static int qpnp_pin_debugfs_create(struct qpnp_pin_chip *q_chip)
  1254. {
  1255. return 0;
  1256. }
  1257. #endif
  1258. static int qpnp_pin_is_valid_pin(struct qpnp_pin_spec *q_spec)
  1259. {
  1260. if (q_spec->type == Q_GPIO_TYPE)
  1261. switch (q_spec->subtype) {
  1262. case Q_GPIO_SUBTYPE_GPIO_4CH:
  1263. case Q_GPIO_SUBTYPE_GPIOC_4CH:
  1264. case Q_GPIO_SUBTYPE_GPIO_8CH:
  1265. case Q_GPIO_SUBTYPE_GPIOC_8CH:
  1266. case Q_GPIO_SUBTYPE_GPIO_LV:
  1267. case Q_GPIO_SUBTYPE_GPIO_MV:
  1268. return 1;
  1269. }
  1270. else if (q_spec->type == Q_MPP_TYPE)
  1271. switch (q_spec->subtype) {
  1272. case Q_MPP_SUBTYPE_4CH_NO_ANA_OUT:
  1273. case Q_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT:
  1274. case Q_MPP_SUBTYPE_4CH_NO_SINK:
  1275. case Q_MPP_SUBTYPE_ULT_4CH_NO_SINK:
  1276. case Q_MPP_SUBTYPE_4CH_FULL_FUNC:
  1277. case Q_MPP_SUBTYPE_8CH_FULL_FUNC:
  1278. return 1;
  1279. }
  1280. return 0;
  1281. }
  1282. static int qpnp_pin_probe(struct platform_device *pdev)
  1283. {
  1284. struct qpnp_pin_chip *q_chip;
  1285. struct qpnp_pin_spec *q_spec;
  1286. unsigned int base;
  1287. struct device_node *child;
  1288. int i, rc;
  1289. u32 lowest_gpio = UINT_MAX, highest_gpio = 0;
  1290. u32 gpio;
  1291. char version[Q_REG_SUBTYPE - Q_REG_DIG_MAJOR_REV + 1];
  1292. const char *pin_dev_name;
  1293. pin_dev_name = dev_name(&pdev->dev);
  1294. if (!pin_dev_name) {
  1295. dev_err(&pdev->dev,
  1296. "%s: label binding undefined for node %s\n",
  1297. __func__,
  1298. pdev->dev.of_node->full_name);
  1299. return -EINVAL;
  1300. }
  1301. q_chip = kzalloc(sizeof(*q_chip), GFP_KERNEL);
  1302. if (!q_chip)
  1303. return -ENOMEM;
  1304. q_chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  1305. if (!q_chip->regmap) {
  1306. dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
  1307. return -EINVAL;
  1308. }
  1309. q_chip->pdev = pdev;
  1310. dev_set_drvdata(&pdev->dev, q_chip);
  1311. mutex_lock(&qpnp_pin_chips_lock);
  1312. list_add(&q_chip->chip_list, &qpnp_pin_chips);
  1313. mutex_unlock(&qpnp_pin_chips_lock);
  1314. /* first scan through nodes to find the range required for allocation */
  1315. i = 0;
  1316. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1317. rc = of_property_read_u32(child, "qcom,pin-num", &gpio);
  1318. if (rc) {
  1319. dev_err(&pdev->dev,
  1320. "%s: unable to get qcom,pin-num property\n",
  1321. __func__);
  1322. goto err_probe;
  1323. }
  1324. if (gpio < lowest_gpio)
  1325. lowest_gpio = gpio;
  1326. if (gpio > highest_gpio)
  1327. highest_gpio = gpio;
  1328. i++;
  1329. }
  1330. q_chip->gpio_chip.ngpio = i;
  1331. if (highest_gpio < lowest_gpio) {
  1332. dev_err(&pdev->dev,
  1333. "%s: no device nodes specified in topology\n",
  1334. __func__);
  1335. rc = -EINVAL;
  1336. goto err_probe;
  1337. } else if (lowest_gpio == 0) {
  1338. dev_err(&pdev->dev, "%s: 0 is not a valid PMIC GPIO\n",
  1339. __func__);
  1340. rc = -EINVAL;
  1341. goto err_probe;
  1342. }
  1343. q_chip->pmic_pin_lowest = lowest_gpio;
  1344. q_chip->pmic_pin_highest = highest_gpio;
  1345. /* allocate gpio lookup tables */
  1346. q_chip->pmic_pins = kzalloc(sizeof(struct qpnp_pin_spec *) *
  1347. (highest_gpio - lowest_gpio + 1),
  1348. GFP_KERNEL);
  1349. q_chip->chip_gpios = kzalloc(sizeof(struct qpnp_pin_spec *) *
  1350. q_chip->gpio_chip.ngpio,
  1351. GFP_KERNEL);
  1352. if (!q_chip->pmic_pins || !q_chip->chip_gpios) {
  1353. dev_err(&pdev->dev, "%s: unable to allocate memory\n",
  1354. __func__);
  1355. rc = -ENOMEM;
  1356. goto err_probe;
  1357. }
  1358. /* get interrupt controller device_node */
  1359. q_chip->int_ctrl = of_irq_find_parent(pdev->dev.of_node);
  1360. if (!q_chip->int_ctrl) {
  1361. dev_err(&pdev->dev, "%s: Can't find interrupt parent\n",
  1362. __func__);
  1363. rc = -EINVAL;
  1364. goto err_probe;
  1365. }
  1366. i = 0;
  1367. /* now scan through again and populate the lookup table */
  1368. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1369. rc = of_property_read_u32(child, "reg", &base);
  1370. if (rc < 0) {
  1371. dev_err(&pdev->dev,
  1372. "Couldn't find reg in node = %s rc = %d\n",
  1373. child->full_name, rc);
  1374. goto err_probe;
  1375. }
  1376. rc = of_property_read_u32(child, "qcom,pin-num", &gpio);
  1377. if (rc) {
  1378. dev_err(&pdev->dev,
  1379. "%s: unable to get qcom,pin-num property\n",
  1380. __func__);
  1381. goto err_probe;
  1382. }
  1383. q_spec = kzalloc(sizeof(struct qpnp_pin_spec), GFP_KERNEL);
  1384. if (!q_spec) {
  1385. rc = -ENOMEM;
  1386. goto err_probe;
  1387. }
  1388. q_spec->slave = to_spmi_device(pdev->dev.parent)->usid;
  1389. q_spec->offset = base;
  1390. q_spec->gpio_chip_idx = i;
  1391. q_spec->pmic_pin = gpio;
  1392. q_spec->node = child;
  1393. q_spec->q_chip = q_chip;
  1394. rc = regmap_bulk_read(q_chip->regmap,
  1395. Q_REG_ADDR(q_spec, Q_REG_DIG_MAJOR_REV),
  1396. &version[0],
  1397. ARRAY_SIZE(version));
  1398. if (rc) {
  1399. dev_err(&pdev->dev, "%s: unable to read type regs\n",
  1400. __func__);
  1401. goto err_probe;
  1402. }
  1403. q_spec->dig_major_rev = version[Q_REG_DIG_MAJOR_REV -
  1404. Q_REG_DIG_MAJOR_REV];
  1405. q_spec->type = version[Q_REG_TYPE - Q_REG_DIG_MAJOR_REV];
  1406. q_spec->subtype = version[Q_REG_SUBTYPE - Q_REG_DIG_MAJOR_REV];
  1407. if (!qpnp_pin_is_valid_pin(q_spec)) {
  1408. dev_err(&pdev->dev,
  1409. "%s: invalid pin type (type=0x%x subtype=0x%x)\n",
  1410. __func__, q_spec->type, q_spec->subtype);
  1411. goto err_probe;
  1412. }
  1413. rc = qpnp_pin_ctl_regs_init(q_spec);
  1414. if (rc)
  1415. goto err_probe;
  1416. /* initialize lookup table params */
  1417. qpnp_pmic_pin_set_spec(q_chip, gpio, q_spec);
  1418. qpnp_chip_gpio_set_spec(q_chip, i, q_spec);
  1419. i++;
  1420. }
  1421. q_chip->gpio_chip.base = -1;
  1422. q_chip->gpio_chip.label = pin_dev_name;
  1423. q_chip->gpio_chip.direction_input = qpnp_pin_direction_input;
  1424. q_chip->gpio_chip.direction_output = qpnp_pin_direction_output;
  1425. q_chip->gpio_chip.to_irq = qpnp_pin_to_irq;
  1426. q_chip->gpio_chip.get = qpnp_pin_get;
  1427. q_chip->gpio_chip.set = qpnp_pin_set;
  1428. q_chip->gpio_chip.parent = &pdev->dev;
  1429. q_chip->gpio_chip.of_xlate = qpnp_pin_of_gpio_xlate;
  1430. q_chip->gpio_chip.of_gpio_n_cells = 2;
  1431. q_chip->gpio_chip.can_sleep = 0;
  1432. rc = gpiochip_add_data(&q_chip->gpio_chip, q_chip);
  1433. if (rc) {
  1434. dev_err(&pdev->dev, "%s: Can't add gpio chip, rc = %d\n",
  1435. __func__, rc);
  1436. goto err_probe;
  1437. }
  1438. q_chip->chip_registered = true;
  1439. /* now configure gpio config defaults if they exist */
  1440. for (i = 0; i < q_chip->gpio_chip.ngpio; i++) {
  1441. q_spec = qpnp_chip_gpio_get_spec(q_chip, i);
  1442. if (WARN_ON(!q_spec)) {
  1443. rc = -ENODEV;
  1444. goto err_probe;
  1445. }
  1446. rc = qpnp_pin_cache_regs(q_chip, q_spec);
  1447. if (rc)
  1448. goto err_probe;
  1449. rc = qpnp_pin_apply_config(q_chip, q_spec);
  1450. if (rc)
  1451. goto err_probe;
  1452. }
  1453. dev_dbg(&pdev->dev, "%s: gpio_chip registered between %d-%u\n",
  1454. __func__, q_chip->gpio_chip.base,
  1455. (q_chip->gpio_chip.base + q_chip->gpio_chip.ngpio) - 1);
  1456. rc = qpnp_pin_debugfs_create(q_chip);
  1457. if (rc) {
  1458. dev_err(&pdev->dev, "%s: debugfs creation failed\n",
  1459. __func__);
  1460. goto err_probe;
  1461. }
  1462. return 0;
  1463. err_probe:
  1464. qpnp_pin_free_chip(q_chip);
  1465. return rc;
  1466. }
  1467. static int qpnp_pin_remove(struct platform_device *pdev)
  1468. {
  1469. struct qpnp_pin_chip *q_chip = dev_get_drvdata(&pdev->dev);
  1470. debugfs_remove_recursive(q_chip->dfs_dir);
  1471. return qpnp_pin_free_chip(q_chip);
  1472. }
  1473. static const struct of_device_id spmi_match_table[] = {
  1474. { .compatible = "qcom,qpnp-pin",
  1475. },
  1476. {}
  1477. };
  1478. static const struct platform_device_id qpnp_pin_id[] = {
  1479. { "qcom,qpnp-pin", 0 },
  1480. { }
  1481. };
  1482. MODULE_DEVICE_TABLE(spmi, qpnp_pin_id);
  1483. static struct platform_driver qpnp_pin_driver = {
  1484. .driver = {
  1485. .name = "qcom,qpnp-pin",
  1486. .of_match_table = spmi_match_table,
  1487. },
  1488. .probe = qpnp_pin_probe,
  1489. .remove = qpnp_pin_remove,
  1490. .id_table = qpnp_pin_id,
  1491. };
  1492. static int __init qpnp_pin_init(void)
  1493. {
  1494. #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG
  1495. driver_dfs_dir = debugfs_create_dir("qpnp_pin", NULL);
  1496. if (driver_dfs_dir == NULL)
  1497. pr_err("Cannot register top level debugfs directory\n");
  1498. #endif
  1499. return platform_driver_register(&qpnp_pin_driver);
  1500. }
  1501. static void __exit qpnp_pin_exit(void)
  1502. {
  1503. #ifdef CONFIG_GPIO_QPNP_PIN_DEBUG
  1504. debugfs_remove_recursive(driver_dfs_dir);
  1505. #endif
  1506. platform_driver_unregister(&qpnp_pin_driver);
  1507. }
  1508. MODULE_DESCRIPTION("QPNP PMIC gpio driver");
  1509. MODULE_LICENSE("GPL v2");
  1510. subsys_initcall(qpnp_pin_init);
  1511. module_exit(qpnp_pin_exit);