pinctrl-sx150x.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright (c) 2016, BayLibre, SAS. All rights reserved.
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. *
  5. * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
  6. *
  7. * Driver for Semtech SX150X I2C GPIO Expanders
  8. * The handling of the 4-bit chips (SX1501/SX1504/SX1507) is untested.
  9. *
  10. * Author: Gregory Bean <gbean@codeaurora.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 and
  14. * only version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/regmap.h>
  22. #include <linux/i2c.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/gpio/driver.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinctrl.h>
  33. #include <linux/pinctrl/pinmux.h>
  34. #include <linux/pinctrl/pinconf-generic.h>
  35. #include "core.h"
  36. #include "pinconf.h"
  37. #include "pinctrl-utils.h"
  38. /* The chip models of sx150x */
  39. enum {
  40. SX150X_123 = 0,
  41. SX150X_456,
  42. SX150X_789,
  43. };
  44. enum {
  45. SX150X_789_REG_MISC_AUTOCLEAR_OFF = 1 << 0,
  46. SX150X_MAX_REGISTER = 0xad,
  47. SX150X_IRQ_TYPE_EDGE_RISING = 0x1,
  48. SX150X_IRQ_TYPE_EDGE_FALLING = 0x2,
  49. SX150X_789_RESET_KEY1 = 0x12,
  50. SX150X_789_RESET_KEY2 = 0x34,
  51. };
  52. struct sx150x_123_pri {
  53. u8 reg_pld_mode;
  54. u8 reg_pld_table0;
  55. u8 reg_pld_table1;
  56. u8 reg_pld_table2;
  57. u8 reg_pld_table3;
  58. u8 reg_pld_table4;
  59. u8 reg_advanced;
  60. };
  61. struct sx150x_456_pri {
  62. u8 reg_pld_mode;
  63. u8 reg_pld_table0;
  64. u8 reg_pld_table1;
  65. u8 reg_pld_table2;
  66. u8 reg_pld_table3;
  67. u8 reg_pld_table4;
  68. u8 reg_advanced;
  69. };
  70. struct sx150x_789_pri {
  71. u8 reg_drain;
  72. u8 reg_polarity;
  73. u8 reg_clock;
  74. u8 reg_misc;
  75. u8 reg_reset;
  76. u8 ngpios;
  77. };
  78. struct sx150x_device_data {
  79. u8 model;
  80. u8 reg_pullup;
  81. u8 reg_pulldn;
  82. u8 reg_dir;
  83. u8 reg_data;
  84. u8 reg_irq_mask;
  85. u8 reg_irq_src;
  86. u8 reg_sense;
  87. u8 ngpios;
  88. union {
  89. struct sx150x_123_pri x123;
  90. struct sx150x_456_pri x456;
  91. struct sx150x_789_pri x789;
  92. } pri;
  93. const struct pinctrl_pin_desc *pins;
  94. unsigned int npins;
  95. };
  96. struct sx150x_pinctrl {
  97. struct device *dev;
  98. struct i2c_client *client;
  99. struct pinctrl_dev *pctldev;
  100. struct pinctrl_desc pinctrl_desc;
  101. struct gpio_chip gpio;
  102. struct irq_chip irq_chip;
  103. struct regmap *regmap;
  104. struct {
  105. u32 sense;
  106. u32 masked;
  107. } irq;
  108. struct mutex lock;
  109. const struct sx150x_device_data *data;
  110. };
  111. static const struct pinctrl_pin_desc sx150x_4_pins[] = {
  112. PINCTRL_PIN(0, "gpio0"),
  113. PINCTRL_PIN(1, "gpio1"),
  114. PINCTRL_PIN(2, "gpio2"),
  115. PINCTRL_PIN(3, "gpio3"),
  116. PINCTRL_PIN(4, "oscio"),
  117. };
  118. static const struct pinctrl_pin_desc sx150x_8_pins[] = {
  119. PINCTRL_PIN(0, "gpio0"),
  120. PINCTRL_PIN(1, "gpio1"),
  121. PINCTRL_PIN(2, "gpio2"),
  122. PINCTRL_PIN(3, "gpio3"),
  123. PINCTRL_PIN(4, "gpio4"),
  124. PINCTRL_PIN(5, "gpio5"),
  125. PINCTRL_PIN(6, "gpio6"),
  126. PINCTRL_PIN(7, "gpio7"),
  127. PINCTRL_PIN(8, "oscio"),
  128. };
  129. static const struct pinctrl_pin_desc sx150x_16_pins[] = {
  130. PINCTRL_PIN(0, "gpio0"),
  131. PINCTRL_PIN(1, "gpio1"),
  132. PINCTRL_PIN(2, "gpio2"),
  133. PINCTRL_PIN(3, "gpio3"),
  134. PINCTRL_PIN(4, "gpio4"),
  135. PINCTRL_PIN(5, "gpio5"),
  136. PINCTRL_PIN(6, "gpio6"),
  137. PINCTRL_PIN(7, "gpio7"),
  138. PINCTRL_PIN(8, "gpio8"),
  139. PINCTRL_PIN(9, "gpio9"),
  140. PINCTRL_PIN(10, "gpio10"),
  141. PINCTRL_PIN(11, "gpio11"),
  142. PINCTRL_PIN(12, "gpio12"),
  143. PINCTRL_PIN(13, "gpio13"),
  144. PINCTRL_PIN(14, "gpio14"),
  145. PINCTRL_PIN(15, "gpio15"),
  146. PINCTRL_PIN(16, "oscio"),
  147. };
  148. static const struct sx150x_device_data sx1501q_device_data = {
  149. .model = SX150X_123,
  150. .reg_pullup = 0x02,
  151. .reg_pulldn = 0x03,
  152. .reg_dir = 0x01,
  153. .reg_data = 0x00,
  154. .reg_irq_mask = 0x05,
  155. .reg_irq_src = 0x08,
  156. .reg_sense = 0x07,
  157. .pri.x123 = {
  158. .reg_pld_mode = 0x10,
  159. .reg_pld_table0 = 0x11,
  160. .reg_pld_table2 = 0x13,
  161. .reg_advanced = 0xad,
  162. },
  163. .ngpios = 4,
  164. .pins = sx150x_4_pins,
  165. .npins = 4, /* oscio not available */
  166. };
  167. static const struct sx150x_device_data sx1502q_device_data = {
  168. .model = SX150X_123,
  169. .reg_pullup = 0x02,
  170. .reg_pulldn = 0x03,
  171. .reg_dir = 0x01,
  172. .reg_data = 0x00,
  173. .reg_irq_mask = 0x05,
  174. .reg_irq_src = 0x08,
  175. .reg_sense = 0x06,
  176. .pri.x123 = {
  177. .reg_pld_mode = 0x10,
  178. .reg_pld_table0 = 0x11,
  179. .reg_pld_table1 = 0x12,
  180. .reg_pld_table2 = 0x13,
  181. .reg_pld_table3 = 0x14,
  182. .reg_pld_table4 = 0x15,
  183. .reg_advanced = 0xad,
  184. },
  185. .ngpios = 8,
  186. .pins = sx150x_8_pins,
  187. .npins = 8, /* oscio not available */
  188. };
  189. static const struct sx150x_device_data sx1503q_device_data = {
  190. .model = SX150X_123,
  191. .reg_pullup = 0x04,
  192. .reg_pulldn = 0x06,
  193. .reg_dir = 0x02,
  194. .reg_data = 0x00,
  195. .reg_irq_mask = 0x08,
  196. .reg_irq_src = 0x0e,
  197. .reg_sense = 0x0a,
  198. .pri.x123 = {
  199. .reg_pld_mode = 0x20,
  200. .reg_pld_table0 = 0x22,
  201. .reg_pld_table1 = 0x24,
  202. .reg_pld_table2 = 0x26,
  203. .reg_pld_table3 = 0x28,
  204. .reg_pld_table4 = 0x2a,
  205. .reg_advanced = 0xad,
  206. },
  207. .ngpios = 16,
  208. .pins = sx150x_16_pins,
  209. .npins = 16, /* oscio not available */
  210. };
  211. static const struct sx150x_device_data sx1504q_device_data = {
  212. .model = SX150X_456,
  213. .reg_pullup = 0x02,
  214. .reg_pulldn = 0x03,
  215. .reg_dir = 0x01,
  216. .reg_data = 0x00,
  217. .reg_irq_mask = 0x05,
  218. .reg_irq_src = 0x08,
  219. .reg_sense = 0x07,
  220. .pri.x456 = {
  221. .reg_pld_mode = 0x10,
  222. .reg_pld_table0 = 0x11,
  223. .reg_pld_table2 = 0x13,
  224. },
  225. .ngpios = 4,
  226. .pins = sx150x_4_pins,
  227. .npins = 4, /* oscio not available */
  228. };
  229. static const struct sx150x_device_data sx1505q_device_data = {
  230. .model = SX150X_456,
  231. .reg_pullup = 0x02,
  232. .reg_pulldn = 0x03,
  233. .reg_dir = 0x01,
  234. .reg_data = 0x00,
  235. .reg_irq_mask = 0x05,
  236. .reg_irq_src = 0x08,
  237. .reg_sense = 0x06,
  238. .pri.x456 = {
  239. .reg_pld_mode = 0x10,
  240. .reg_pld_table0 = 0x11,
  241. .reg_pld_table1 = 0x12,
  242. .reg_pld_table2 = 0x13,
  243. .reg_pld_table3 = 0x14,
  244. .reg_pld_table4 = 0x15,
  245. },
  246. .ngpios = 8,
  247. .pins = sx150x_8_pins,
  248. .npins = 8, /* oscio not available */
  249. };
  250. static const struct sx150x_device_data sx1506q_device_data = {
  251. .model = SX150X_456,
  252. .reg_pullup = 0x04,
  253. .reg_pulldn = 0x06,
  254. .reg_dir = 0x02,
  255. .reg_data = 0x00,
  256. .reg_irq_mask = 0x08,
  257. .reg_irq_src = 0x0e,
  258. .reg_sense = 0x0a,
  259. .pri.x456 = {
  260. .reg_pld_mode = 0x20,
  261. .reg_pld_table0 = 0x22,
  262. .reg_pld_table1 = 0x24,
  263. .reg_pld_table2 = 0x26,
  264. .reg_pld_table3 = 0x28,
  265. .reg_pld_table4 = 0x2a,
  266. .reg_advanced = 0xad,
  267. },
  268. .ngpios = 16,
  269. .pins = sx150x_16_pins,
  270. .npins = 16, /* oscio not available */
  271. };
  272. static const struct sx150x_device_data sx1507q_device_data = {
  273. .model = SX150X_789,
  274. .reg_pullup = 0x03,
  275. .reg_pulldn = 0x04,
  276. .reg_dir = 0x07,
  277. .reg_data = 0x08,
  278. .reg_irq_mask = 0x09,
  279. .reg_irq_src = 0x0b,
  280. .reg_sense = 0x0a,
  281. .pri.x789 = {
  282. .reg_drain = 0x05,
  283. .reg_polarity = 0x06,
  284. .reg_clock = 0x0d,
  285. .reg_misc = 0x0e,
  286. .reg_reset = 0x7d,
  287. },
  288. .ngpios = 4,
  289. .pins = sx150x_4_pins,
  290. .npins = ARRAY_SIZE(sx150x_4_pins),
  291. };
  292. static const struct sx150x_device_data sx1508q_device_data = {
  293. .model = SX150X_789,
  294. .reg_pullup = 0x03,
  295. .reg_pulldn = 0x04,
  296. .reg_dir = 0x07,
  297. .reg_data = 0x08,
  298. .reg_irq_mask = 0x09,
  299. .reg_irq_src = 0x0c,
  300. .reg_sense = 0x0a,
  301. .pri.x789 = {
  302. .reg_drain = 0x05,
  303. .reg_polarity = 0x06,
  304. .reg_clock = 0x0f,
  305. .reg_misc = 0x10,
  306. .reg_reset = 0x7d,
  307. },
  308. .ngpios = 8,
  309. .pins = sx150x_8_pins,
  310. .npins = ARRAY_SIZE(sx150x_8_pins),
  311. };
  312. static const struct sx150x_device_data sx1509q_device_data = {
  313. .model = SX150X_789,
  314. .reg_pullup = 0x06,
  315. .reg_pulldn = 0x08,
  316. .reg_dir = 0x0e,
  317. .reg_data = 0x10,
  318. .reg_irq_mask = 0x12,
  319. .reg_irq_src = 0x18,
  320. .reg_sense = 0x14,
  321. .pri.x789 = {
  322. .reg_drain = 0x0a,
  323. .reg_polarity = 0x0c,
  324. .reg_clock = 0x1e,
  325. .reg_misc = 0x1f,
  326. .reg_reset = 0x7d,
  327. },
  328. .ngpios = 16,
  329. .pins = sx150x_16_pins,
  330. .npins = ARRAY_SIZE(sx150x_16_pins),
  331. };
  332. static int sx150x_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  333. {
  334. return 0;
  335. }
  336. static const char *sx150x_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  337. unsigned int group)
  338. {
  339. return NULL;
  340. }
  341. static int sx150x_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  342. unsigned int group,
  343. const unsigned int **pins,
  344. unsigned int *num_pins)
  345. {
  346. return -ENOTSUPP;
  347. }
  348. static const struct pinctrl_ops sx150x_pinctrl_ops = {
  349. .get_groups_count = sx150x_pinctrl_get_groups_count,
  350. .get_group_name = sx150x_pinctrl_get_group_name,
  351. .get_group_pins = sx150x_pinctrl_get_group_pins,
  352. #ifdef CONFIG_OF
  353. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  354. .dt_free_map = pinctrl_utils_free_map,
  355. #endif
  356. };
  357. static bool sx150x_pin_is_oscio(struct sx150x_pinctrl *pctl, unsigned int pin)
  358. {
  359. if (pin >= pctl->data->npins)
  360. return false;
  361. /* OSCIO pin is only present in 789 devices */
  362. if (pctl->data->model != SX150X_789)
  363. return false;
  364. return !strcmp(pctl->data->pins[pin].name, "oscio");
  365. }
  366. static int sx150x_gpio_get_direction(struct gpio_chip *chip,
  367. unsigned int offset)
  368. {
  369. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  370. unsigned int value;
  371. int ret;
  372. if (sx150x_pin_is_oscio(pctl, offset))
  373. return false;
  374. ret = regmap_read(pctl->regmap, pctl->data->reg_dir, &value);
  375. if (ret < 0)
  376. return ret;
  377. return !!(value & BIT(offset));
  378. }
  379. static int sx150x_gpio_get(struct gpio_chip *chip, unsigned int offset)
  380. {
  381. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  382. unsigned int value;
  383. int ret;
  384. if (sx150x_pin_is_oscio(pctl, offset))
  385. return -EINVAL;
  386. ret = regmap_read(pctl->regmap, pctl->data->reg_data, &value);
  387. if (ret < 0)
  388. return ret;
  389. return !!(value & BIT(offset));
  390. }
  391. static int __sx150x_gpio_set(struct sx150x_pinctrl *pctl, unsigned int offset,
  392. int value)
  393. {
  394. return regmap_write_bits(pctl->regmap, pctl->data->reg_data,
  395. BIT(offset), value ? BIT(offset) : 0);
  396. }
  397. static int sx150x_gpio_oscio_set(struct sx150x_pinctrl *pctl,
  398. int value)
  399. {
  400. return regmap_write(pctl->regmap,
  401. pctl->data->pri.x789.reg_clock,
  402. (value ? 0x1f : 0x10));
  403. }
  404. static void sx150x_gpio_set(struct gpio_chip *chip, unsigned int offset,
  405. int value)
  406. {
  407. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  408. if (sx150x_pin_is_oscio(pctl, offset))
  409. sx150x_gpio_oscio_set(pctl, value);
  410. else
  411. __sx150x_gpio_set(pctl, offset, value);
  412. }
  413. static void sx150x_gpio_set_multiple(struct gpio_chip *chip,
  414. unsigned long *mask,
  415. unsigned long *bits)
  416. {
  417. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  418. regmap_write_bits(pctl->regmap, pctl->data->reg_data, *mask, *bits);
  419. }
  420. static int sx150x_gpio_direction_input(struct gpio_chip *chip,
  421. unsigned int offset)
  422. {
  423. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  424. if (sx150x_pin_is_oscio(pctl, offset))
  425. return -EINVAL;
  426. return regmap_write_bits(pctl->regmap,
  427. pctl->data->reg_dir,
  428. BIT(offset), BIT(offset));
  429. }
  430. static int sx150x_gpio_direction_output(struct gpio_chip *chip,
  431. unsigned int offset, int value)
  432. {
  433. struct sx150x_pinctrl *pctl = gpiochip_get_data(chip);
  434. int ret;
  435. if (sx150x_pin_is_oscio(pctl, offset))
  436. return sx150x_gpio_oscio_set(pctl, value);
  437. ret = __sx150x_gpio_set(pctl, offset, value);
  438. if (ret < 0)
  439. return ret;
  440. return regmap_write_bits(pctl->regmap,
  441. pctl->data->reg_dir,
  442. BIT(offset), 0);
  443. }
  444. static void sx150x_irq_mask(struct irq_data *d)
  445. {
  446. struct sx150x_pinctrl *pctl =
  447. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  448. unsigned int n = d->hwirq;
  449. pctl->irq.masked |= BIT(n);
  450. }
  451. static void sx150x_irq_unmask(struct irq_data *d)
  452. {
  453. struct sx150x_pinctrl *pctl =
  454. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  455. unsigned int n = d->hwirq;
  456. pctl->irq.masked &= ~BIT(n);
  457. }
  458. static void sx150x_irq_set_sense(struct sx150x_pinctrl *pctl,
  459. unsigned int line, unsigned int sense)
  460. {
  461. /*
  462. * Every interrupt line is represented by two bits shifted
  463. * proportionally to the line number
  464. */
  465. const unsigned int n = line * 2;
  466. const unsigned int mask = ~((SX150X_IRQ_TYPE_EDGE_RISING |
  467. SX150X_IRQ_TYPE_EDGE_FALLING) << n);
  468. pctl->irq.sense &= mask;
  469. pctl->irq.sense |= sense << n;
  470. }
  471. static int sx150x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  472. {
  473. struct sx150x_pinctrl *pctl =
  474. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  475. unsigned int n, val = 0;
  476. if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  477. return -EINVAL;
  478. n = d->hwirq;
  479. if (flow_type & IRQ_TYPE_EDGE_RISING)
  480. val |= SX150X_IRQ_TYPE_EDGE_RISING;
  481. if (flow_type & IRQ_TYPE_EDGE_FALLING)
  482. val |= SX150X_IRQ_TYPE_EDGE_FALLING;
  483. sx150x_irq_set_sense(pctl, n, val);
  484. return 0;
  485. }
  486. static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
  487. {
  488. struct sx150x_pinctrl *pctl = (struct sx150x_pinctrl *)dev_id;
  489. unsigned long n, status;
  490. unsigned int val;
  491. int err;
  492. err = regmap_read(pctl->regmap, pctl->data->reg_irq_src, &val);
  493. if (err < 0)
  494. return IRQ_NONE;
  495. err = regmap_write(pctl->regmap, pctl->data->reg_irq_src, val);
  496. if (err < 0)
  497. return IRQ_NONE;
  498. status = val;
  499. for_each_set_bit(n, &status, pctl->data->ngpios)
  500. handle_nested_irq(irq_find_mapping(pctl->gpio.irq.domain, n));
  501. return IRQ_HANDLED;
  502. }
  503. static void sx150x_irq_bus_lock(struct irq_data *d)
  504. {
  505. struct sx150x_pinctrl *pctl =
  506. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  507. mutex_lock(&pctl->lock);
  508. }
  509. static void sx150x_irq_bus_sync_unlock(struct irq_data *d)
  510. {
  511. struct sx150x_pinctrl *pctl =
  512. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  513. regmap_write(pctl->regmap, pctl->data->reg_irq_mask, pctl->irq.masked);
  514. regmap_write(pctl->regmap, pctl->data->reg_sense, pctl->irq.sense);
  515. mutex_unlock(&pctl->lock);
  516. }
  517. static int sx150x_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  518. unsigned long *config)
  519. {
  520. struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  521. unsigned int param = pinconf_to_config_param(*config);
  522. int ret;
  523. u32 arg;
  524. unsigned int data;
  525. if (sx150x_pin_is_oscio(pctl, pin)) {
  526. switch (param) {
  527. case PIN_CONFIG_DRIVE_PUSH_PULL:
  528. case PIN_CONFIG_OUTPUT:
  529. ret = regmap_read(pctl->regmap,
  530. pctl->data->pri.x789.reg_clock,
  531. &data);
  532. if (ret < 0)
  533. return ret;
  534. if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
  535. arg = (data & 0x1f) ? 1 : 0;
  536. else {
  537. if ((data & 0x1f) == 0x1f)
  538. arg = 1;
  539. else if ((data & 0x1f) == 0x10)
  540. arg = 0;
  541. else
  542. return -EINVAL;
  543. }
  544. break;
  545. default:
  546. return -ENOTSUPP;
  547. }
  548. goto out;
  549. }
  550. switch (param) {
  551. case PIN_CONFIG_BIAS_PULL_DOWN:
  552. ret = regmap_read(pctl->regmap,
  553. pctl->data->reg_pulldn,
  554. &data);
  555. data &= BIT(pin);
  556. if (ret < 0)
  557. return ret;
  558. if (!ret)
  559. return -EINVAL;
  560. arg = 1;
  561. break;
  562. case PIN_CONFIG_BIAS_PULL_UP:
  563. ret = regmap_read(pctl->regmap,
  564. pctl->data->reg_pullup,
  565. &data);
  566. data &= BIT(pin);
  567. if (ret < 0)
  568. return ret;
  569. if (!ret)
  570. return -EINVAL;
  571. arg = 1;
  572. break;
  573. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  574. if (pctl->data->model != SX150X_789)
  575. return -ENOTSUPP;
  576. ret = regmap_read(pctl->regmap,
  577. pctl->data->pri.x789.reg_drain,
  578. &data);
  579. data &= BIT(pin);
  580. if (ret < 0)
  581. return ret;
  582. if (!data)
  583. return -EINVAL;
  584. arg = 1;
  585. break;
  586. case PIN_CONFIG_DRIVE_PUSH_PULL:
  587. if (pctl->data->model != SX150X_789)
  588. arg = true;
  589. else {
  590. ret = regmap_read(pctl->regmap,
  591. pctl->data->pri.x789.reg_drain,
  592. &data);
  593. data &= BIT(pin);
  594. if (ret < 0)
  595. return ret;
  596. if (data)
  597. return -EINVAL;
  598. arg = 1;
  599. }
  600. break;
  601. case PIN_CONFIG_OUTPUT:
  602. ret = sx150x_gpio_get_direction(&pctl->gpio, pin);
  603. if (ret < 0)
  604. return ret;
  605. if (ret)
  606. return -EINVAL;
  607. ret = sx150x_gpio_get(&pctl->gpio, pin);
  608. if (ret < 0)
  609. return ret;
  610. arg = ret;
  611. break;
  612. default:
  613. return -ENOTSUPP;
  614. }
  615. out:
  616. *config = pinconf_to_config_packed(param, arg);
  617. return 0;
  618. }
  619. static int sx150x_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  620. unsigned long *configs, unsigned int num_configs)
  621. {
  622. struct sx150x_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  623. enum pin_config_param param;
  624. u32 arg;
  625. int i;
  626. int ret;
  627. for (i = 0; i < num_configs; i++) {
  628. param = pinconf_to_config_param(configs[i]);
  629. arg = pinconf_to_config_argument(configs[i]);
  630. if (sx150x_pin_is_oscio(pctl, pin)) {
  631. if (param == PIN_CONFIG_OUTPUT) {
  632. ret = sx150x_gpio_direction_output(&pctl->gpio,
  633. pin, arg);
  634. if (ret < 0)
  635. return ret;
  636. continue;
  637. } else
  638. return -ENOTSUPP;
  639. }
  640. switch (param) {
  641. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  642. case PIN_CONFIG_BIAS_DISABLE:
  643. ret = regmap_write_bits(pctl->regmap,
  644. pctl->data->reg_pulldn,
  645. BIT(pin), 0);
  646. if (ret < 0)
  647. return ret;
  648. ret = regmap_write_bits(pctl->regmap,
  649. pctl->data->reg_pullup,
  650. BIT(pin), 0);
  651. if (ret < 0)
  652. return ret;
  653. break;
  654. case PIN_CONFIG_BIAS_PULL_UP:
  655. ret = regmap_write_bits(pctl->regmap,
  656. pctl->data->reg_pullup,
  657. BIT(pin), BIT(pin));
  658. if (ret < 0)
  659. return ret;
  660. break;
  661. case PIN_CONFIG_BIAS_PULL_DOWN:
  662. ret = regmap_write_bits(pctl->regmap,
  663. pctl->data->reg_pulldn,
  664. BIT(pin), BIT(pin));
  665. if (ret < 0)
  666. return ret;
  667. break;
  668. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  669. if (pctl->data->model != SX150X_789 ||
  670. sx150x_pin_is_oscio(pctl, pin))
  671. return -ENOTSUPP;
  672. ret = regmap_write_bits(pctl->regmap,
  673. pctl->data->pri.x789.reg_drain,
  674. BIT(pin), BIT(pin));
  675. if (ret < 0)
  676. return ret;
  677. break;
  678. case PIN_CONFIG_DRIVE_PUSH_PULL:
  679. if (pctl->data->model != SX150X_789 ||
  680. sx150x_pin_is_oscio(pctl, pin))
  681. return 0;
  682. ret = regmap_write_bits(pctl->regmap,
  683. pctl->data->pri.x789.reg_drain,
  684. BIT(pin), 0);
  685. if (ret < 0)
  686. return ret;
  687. break;
  688. case PIN_CONFIG_OUTPUT:
  689. ret = sx150x_gpio_direction_output(&pctl->gpio,
  690. pin, arg);
  691. if (ret < 0)
  692. return ret;
  693. break;
  694. default:
  695. return -ENOTSUPP;
  696. }
  697. } /* for each config */
  698. return 0;
  699. }
  700. static const struct pinconf_ops sx150x_pinconf_ops = {
  701. .pin_config_get = sx150x_pinconf_get,
  702. .pin_config_set = sx150x_pinconf_set,
  703. .is_generic = true,
  704. };
  705. static const struct i2c_device_id sx150x_id[] = {
  706. {"sx1501q", (kernel_ulong_t) &sx1501q_device_data },
  707. {"sx1502q", (kernel_ulong_t) &sx1502q_device_data },
  708. {"sx1503q", (kernel_ulong_t) &sx1503q_device_data },
  709. {"sx1504q", (kernel_ulong_t) &sx1504q_device_data },
  710. {"sx1505q", (kernel_ulong_t) &sx1505q_device_data },
  711. {"sx1506q", (kernel_ulong_t) &sx1506q_device_data },
  712. {"sx1507q", (kernel_ulong_t) &sx1507q_device_data },
  713. {"sx1508q", (kernel_ulong_t) &sx1508q_device_data },
  714. {"sx1509q", (kernel_ulong_t) &sx1509q_device_data },
  715. {}
  716. };
  717. static const struct of_device_id sx150x_of_match[] = {
  718. { .compatible = "semtech,sx1501q", .data = &sx1501q_device_data },
  719. { .compatible = "semtech,sx1502q", .data = &sx1502q_device_data },
  720. { .compatible = "semtech,sx1503q", .data = &sx1503q_device_data },
  721. { .compatible = "semtech,sx1504q", .data = &sx1504q_device_data },
  722. { .compatible = "semtech,sx1505q", .data = &sx1505q_device_data },
  723. { .compatible = "semtech,sx1506q", .data = &sx1506q_device_data },
  724. { .compatible = "semtech,sx1507q", .data = &sx1507q_device_data },
  725. { .compatible = "semtech,sx1508q", .data = &sx1508q_device_data },
  726. { .compatible = "semtech,sx1509q", .data = &sx1509q_device_data },
  727. {},
  728. };
  729. static int sx150x_reset(struct sx150x_pinctrl *pctl)
  730. {
  731. int err;
  732. err = i2c_smbus_write_byte_data(pctl->client,
  733. pctl->data->pri.x789.reg_reset,
  734. SX150X_789_RESET_KEY1);
  735. if (err < 0)
  736. return err;
  737. err = i2c_smbus_write_byte_data(pctl->client,
  738. pctl->data->pri.x789.reg_reset,
  739. SX150X_789_RESET_KEY2);
  740. return err;
  741. }
  742. static int sx150x_init_misc(struct sx150x_pinctrl *pctl)
  743. {
  744. u8 reg, value;
  745. switch (pctl->data->model) {
  746. case SX150X_789:
  747. reg = pctl->data->pri.x789.reg_misc;
  748. value = SX150X_789_REG_MISC_AUTOCLEAR_OFF;
  749. break;
  750. case SX150X_456:
  751. reg = pctl->data->pri.x456.reg_advanced;
  752. value = 0x00;
  753. /*
  754. * Only SX1506 has RegAdvanced, SX1504/5 are expected
  755. * to initialize this offset to zero
  756. */
  757. if (!reg)
  758. return 0;
  759. break;
  760. case SX150X_123:
  761. reg = pctl->data->pri.x123.reg_advanced;
  762. value = 0x00;
  763. break;
  764. default:
  765. WARN(1, "Unknown chip model %d\n", pctl->data->model);
  766. return -EINVAL;
  767. }
  768. return regmap_write(pctl->regmap, reg, value);
  769. }
  770. static int sx150x_init_hw(struct sx150x_pinctrl *pctl)
  771. {
  772. const u8 reg[] = {
  773. [SX150X_789] = pctl->data->pri.x789.reg_polarity,
  774. [SX150X_456] = pctl->data->pri.x456.reg_pld_mode,
  775. [SX150X_123] = pctl->data->pri.x123.reg_pld_mode,
  776. };
  777. int err;
  778. if (pctl->data->model == SX150X_789 &&
  779. of_property_read_bool(pctl->dev->of_node, "semtech,probe-reset")) {
  780. err = sx150x_reset(pctl);
  781. if (err < 0)
  782. return err;
  783. }
  784. err = sx150x_init_misc(pctl);
  785. if (err < 0)
  786. return err;
  787. /* Set all pins to work in normal mode */
  788. return regmap_write(pctl->regmap, reg[pctl->data->model], 0);
  789. }
  790. static int sx150x_regmap_reg_width(struct sx150x_pinctrl *pctl,
  791. unsigned int reg)
  792. {
  793. const struct sx150x_device_data *data = pctl->data;
  794. if (reg == data->reg_sense) {
  795. /*
  796. * RegSense packs two bits of configuration per GPIO,
  797. * so we'd need to read twice as many bits as there
  798. * are GPIO in our chip
  799. */
  800. return 2 * data->ngpios;
  801. } else if ((data->model == SX150X_789 &&
  802. (reg == data->pri.x789.reg_misc ||
  803. reg == data->pri.x789.reg_clock ||
  804. reg == data->pri.x789.reg_reset))
  805. ||
  806. (data->model == SX150X_123 &&
  807. reg == data->pri.x123.reg_advanced)
  808. ||
  809. (data->model == SX150X_456 &&
  810. data->pri.x456.reg_advanced &&
  811. reg == data->pri.x456.reg_advanced)) {
  812. return 8;
  813. } else {
  814. return data->ngpios;
  815. }
  816. }
  817. static unsigned int sx150x_maybe_swizzle(struct sx150x_pinctrl *pctl,
  818. unsigned int reg, unsigned int val)
  819. {
  820. unsigned int a, b;
  821. const struct sx150x_device_data *data = pctl->data;
  822. /*
  823. * Whereas SX1509 presents RegSense in a simple layout as such:
  824. * reg [ f f e e d d c c ]
  825. * reg + 1 [ b b a a 9 9 8 8 ]
  826. * reg + 2 [ 7 7 6 6 5 5 4 4 ]
  827. * reg + 3 [ 3 3 2 2 1 1 0 0 ]
  828. *
  829. * SX1503 and SX1506 deviate from that data layout, instead storing
  830. * their contents as follows:
  831. *
  832. * reg [ f f e e d d c c ]
  833. * reg + 1 [ 7 7 6 6 5 5 4 4 ]
  834. * reg + 2 [ b b a a 9 9 8 8 ]
  835. * reg + 3 [ 3 3 2 2 1 1 0 0 ]
  836. *
  837. * so, taking that into account, we swap two
  838. * inner bytes of a 4-byte result
  839. */
  840. if (reg == data->reg_sense &&
  841. data->ngpios == 16 &&
  842. (data->model == SX150X_123 ||
  843. data->model == SX150X_456)) {
  844. a = val & 0x00ff0000;
  845. b = val & 0x0000ff00;
  846. val &= 0xff0000ff;
  847. val |= b << 8;
  848. val |= a >> 8;
  849. }
  850. return val;
  851. }
  852. /*
  853. * In order to mask the differences between 16 and 8 bit expander
  854. * devices we set up a sligthly ficticious regmap that pretends to be
  855. * a set of 32-bit (to accomodate RegSenseLow/RegSenseHigh
  856. * pair/quartet) registers and transparently reconstructs those
  857. * registers via multiple I2C/SMBus reads
  858. *
  859. * This way the rest of the driver code, interfacing with the chip via
  860. * regmap API, can work assuming that each GPIO pin is represented by
  861. * a group of bits at an offset proportional to GPIO number within a
  862. * given register.
  863. */
  864. static int sx150x_regmap_reg_read(void *context, unsigned int reg,
  865. unsigned int *result)
  866. {
  867. int ret, n;
  868. struct sx150x_pinctrl *pctl = context;
  869. struct i2c_client *i2c = pctl->client;
  870. const int width = sx150x_regmap_reg_width(pctl, reg);
  871. unsigned int idx, val;
  872. /*
  873. * There are four potential cases covered by this function:
  874. *
  875. * 1) 8-pin chip, single configuration bit register
  876. *
  877. * This is trivial the code below just needs to read:
  878. * reg [ 7 6 5 4 3 2 1 0 ]
  879. *
  880. * 2) 8-pin chip, double configuration bit register (RegSense)
  881. *
  882. * The read will be done as follows:
  883. * reg [ 7 7 6 6 5 5 4 4 ]
  884. * reg + 1 [ 3 3 2 2 1 1 0 0 ]
  885. *
  886. * 3) 16-pin chip, single configuration bit register
  887. *
  888. * The read will be done as follows:
  889. * reg [ f e d c b a 9 8 ]
  890. * reg + 1 [ 7 6 5 4 3 2 1 0 ]
  891. *
  892. * 4) 16-pin chip, double configuration bit register (RegSense)
  893. *
  894. * The read will be done as follows:
  895. * reg [ f f e e d d c c ]
  896. * reg + 1 [ b b a a 9 9 8 8 ]
  897. * reg + 2 [ 7 7 6 6 5 5 4 4 ]
  898. * reg + 3 [ 3 3 2 2 1 1 0 0 ]
  899. */
  900. for (n = width, val = 0, idx = reg; n > 0; n -= 8, idx++) {
  901. val <<= 8;
  902. ret = i2c_smbus_read_byte_data(i2c, idx);
  903. if (ret < 0)
  904. return ret;
  905. val |= ret;
  906. }
  907. *result = sx150x_maybe_swizzle(pctl, reg, val);
  908. return 0;
  909. }
  910. static int sx150x_regmap_reg_write(void *context, unsigned int reg,
  911. unsigned int val)
  912. {
  913. int ret, n;
  914. struct sx150x_pinctrl *pctl = context;
  915. struct i2c_client *i2c = pctl->client;
  916. const int width = sx150x_regmap_reg_width(pctl, reg);
  917. val = sx150x_maybe_swizzle(pctl, reg, val);
  918. n = (width - 1) & ~7;
  919. do {
  920. const u8 byte = (val >> n) & 0xff;
  921. ret = i2c_smbus_write_byte_data(i2c, reg, byte);
  922. if (ret < 0)
  923. return ret;
  924. reg++;
  925. n -= 8;
  926. } while (n >= 0);
  927. return 0;
  928. }
  929. static bool sx150x_reg_volatile(struct device *dev, unsigned int reg)
  930. {
  931. struct sx150x_pinctrl *pctl = i2c_get_clientdata(to_i2c_client(dev));
  932. return reg == pctl->data->reg_irq_src || reg == pctl->data->reg_data;
  933. }
  934. static const struct regmap_config sx150x_regmap_config = {
  935. .reg_bits = 8,
  936. .val_bits = 32,
  937. .cache_type = REGCACHE_RBTREE,
  938. .reg_read = sx150x_regmap_reg_read,
  939. .reg_write = sx150x_regmap_reg_write,
  940. .max_register = SX150X_MAX_REGISTER,
  941. .volatile_reg = sx150x_reg_volatile,
  942. };
  943. static int sx150x_probe(struct i2c_client *client,
  944. const struct i2c_device_id *id)
  945. {
  946. static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
  947. I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  948. struct device *dev = &client->dev;
  949. struct sx150x_pinctrl *pctl;
  950. int ret;
  951. if (!i2c_check_functionality(client->adapter, i2c_funcs))
  952. return -ENOSYS;
  953. pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
  954. if (!pctl)
  955. return -ENOMEM;
  956. i2c_set_clientdata(client, pctl);
  957. pctl->dev = dev;
  958. pctl->client = client;
  959. if (dev->of_node)
  960. pctl->data = of_device_get_match_data(dev);
  961. else
  962. pctl->data = (struct sx150x_device_data *)id->driver_data;
  963. if (!pctl->data)
  964. return -EINVAL;
  965. pctl->regmap = devm_regmap_init(dev, NULL, pctl,
  966. &sx150x_regmap_config);
  967. if (IS_ERR(pctl->regmap)) {
  968. ret = PTR_ERR(pctl->regmap);
  969. dev_err(dev, "Failed to allocate register map: %d\n",
  970. ret);
  971. return ret;
  972. }
  973. mutex_init(&pctl->lock);
  974. ret = sx150x_init_hw(pctl);
  975. if (ret)
  976. return ret;
  977. /* Pinctrl_desc */
  978. pctl->pinctrl_desc.name = "sx150x-pinctrl";
  979. pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
  980. pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
  981. pctl->pinctrl_desc.pins = pctl->data->pins;
  982. pctl->pinctrl_desc.npins = pctl->data->npins;
  983. pctl->pinctrl_desc.owner = THIS_MODULE;
  984. ret = devm_pinctrl_register_and_init(dev, &pctl->pinctrl_desc,
  985. pctl, &pctl->pctldev);
  986. if (ret) {
  987. dev_err(dev, "Failed to register pinctrl device\n");
  988. return ret;
  989. }
  990. ret = pinctrl_enable(pctl->pctldev);
  991. if (ret) {
  992. dev_err(dev, "Failed to enable pinctrl device\n");
  993. return ret;
  994. }
  995. /* Register GPIO controller */
  996. pctl->gpio.base = -1;
  997. pctl->gpio.ngpio = pctl->data->npins;
  998. pctl->gpio.get_direction = sx150x_gpio_get_direction;
  999. pctl->gpio.direction_input = sx150x_gpio_direction_input;
  1000. pctl->gpio.direction_output = sx150x_gpio_direction_output;
  1001. pctl->gpio.get = sx150x_gpio_get;
  1002. pctl->gpio.set = sx150x_gpio_set;
  1003. pctl->gpio.set_config = gpiochip_generic_config;
  1004. pctl->gpio.parent = dev;
  1005. #ifdef CONFIG_OF_GPIO
  1006. pctl->gpio.of_node = dev->of_node;
  1007. #endif
  1008. pctl->gpio.can_sleep = true;
  1009. pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
  1010. if (!pctl->gpio.label)
  1011. return -ENOMEM;
  1012. /*
  1013. * Setting multiple pins is not safe when all pins are not
  1014. * handled by the same regmap register. The oscio pin (present
  1015. * on the SX150X_789 chips) lives in its own register, so
  1016. * would require locking that is not in place at this time.
  1017. */
  1018. if (pctl->data->model != SX150X_789)
  1019. pctl->gpio.set_multiple = sx150x_gpio_set_multiple;
  1020. ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
  1021. if (ret)
  1022. return ret;
  1023. ret = gpiochip_add_pin_range(&pctl->gpio, dev_name(dev),
  1024. 0, 0, pctl->data->npins);
  1025. if (ret)
  1026. return ret;
  1027. /* Add Interrupt support if an irq is specified */
  1028. if (client->irq > 0) {
  1029. pctl->irq_chip.irq_mask = sx150x_irq_mask;
  1030. pctl->irq_chip.irq_unmask = sx150x_irq_unmask;
  1031. pctl->irq_chip.irq_set_type = sx150x_irq_set_type;
  1032. pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
  1033. pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
  1034. pctl->irq_chip.name = devm_kstrdup(dev, client->name,
  1035. GFP_KERNEL);
  1036. if (!pctl->irq_chip.name)
  1037. return -ENOMEM;
  1038. pctl->irq.masked = ~0;
  1039. pctl->irq.sense = 0;
  1040. /*
  1041. * Because sx150x_irq_threaded_fn invokes all of the
  1042. * nested interrrupt handlers via handle_nested_irq,
  1043. * any "handler" passed to gpiochip_irqchip_add()
  1044. * below is going to be ignored, so the choice of the
  1045. * function does not matter that much.
  1046. *
  1047. * We set it to handle_bad_irq to avoid confusion,
  1048. * plus it will be instantly noticeable if it is ever
  1049. * called (should not happen)
  1050. */
  1051. ret = gpiochip_irqchip_add_nested(&pctl->gpio,
  1052. &pctl->irq_chip, 0,
  1053. handle_bad_irq, IRQ_TYPE_NONE);
  1054. if (ret) {
  1055. dev_err(dev, "could not connect irqchip to gpiochip\n");
  1056. return ret;
  1057. }
  1058. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  1059. sx150x_irq_thread_fn,
  1060. IRQF_ONESHOT | IRQF_SHARED |
  1061. IRQF_TRIGGER_FALLING,
  1062. pctl->irq_chip.name, pctl);
  1063. if (ret < 0)
  1064. return ret;
  1065. gpiochip_set_nested_irqchip(&pctl->gpio,
  1066. &pctl->irq_chip,
  1067. client->irq);
  1068. }
  1069. return 0;
  1070. }
  1071. static struct i2c_driver sx150x_driver = {
  1072. .driver = {
  1073. .name = "sx150x-pinctrl",
  1074. .of_match_table = of_match_ptr(sx150x_of_match),
  1075. },
  1076. .probe = sx150x_probe,
  1077. .id_table = sx150x_id,
  1078. };
  1079. static int __init sx150x_init(void)
  1080. {
  1081. return i2c_add_driver(&sx150x_driver);
  1082. }
  1083. subsys_initcall(sx150x_init);