pinctrl-aw9523.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Awinic AW9523B i2c pin controller driver
  4. * Copyright (c) 2020, AngeloGioacchino Del Regno
  5. * <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/regmap.h>
  9. #include <linux/i2c.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/mutex.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/version.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include <linux/pinctrl/pinmux.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/regulator/consumer.h>
  26. #include "core.h"
  27. #include "pinconf.h"
  28. #include "pinctrl-utils.h"
  29. #define AW9523_MAX_FUNCS 2
  30. #define AW9523_NUM_PORTS 2
  31. #define AW9523_PINS_PER_PORT 8
  32. /*
  33. * HW needs at least 20uS for reset and at least 1-2uS to recover from
  34. * reset, but we have to account for eventual board quirks, if any:
  35. * for this reason, keep reset asserted for 50uS and wait for 20uS
  36. * to recover from the reset.
  37. */
  38. #define AW9523_HW_RESET_US 50
  39. #define AW9523_HW_RESET_RECOVERY_US 20
  40. /* Port 0: P0_0...P0_7 - Port 1: P1_0...P1_7 */
  41. #define AW9523_PIN_TO_PORT(pin) (pin >> 3)
  42. #define AW9523_REG_IN_STATE(pin) (0x00 + AW9523_PIN_TO_PORT(pin))
  43. #define AW9523_REG_OUT_STATE(pin) (0x02 + AW9523_PIN_TO_PORT(pin))
  44. #define AW9523_REG_CONF_STATE(pin) (0x04 + AW9523_PIN_TO_PORT(pin))
  45. #define AW9523_REG_INTR_DIS(pin) (0x06 + AW9523_PIN_TO_PORT(pin))
  46. #define AW9523_REG_CHIPID 0x10
  47. #define AW9523_VAL_EXPECTED_CHIPID 0x23
  48. #define AW9523_REG_GCR 0x11
  49. #define AW9523_GCR_ISEL_MASK GENMASK(0, 1)
  50. #define AW9523_GCR_GPOMD_MASK BIT(4)
  51. #define AW9523_REG_PORT_MODE(pin) (0x12 + AW9523_PIN_TO_PORT(pin))
  52. #define AW9523_REG_SOFT_RESET 0x7f
  53. #define AW9523_VAL_RESET 0x00
  54. /*
  55. * struct aw9523_irq - Interrupt controller structure
  56. * @lock: mutex locking for the irq bus
  57. * @irqchip: structure holding irqchip params
  58. * @cached_gpio: stores the previous gpio status for bit comparison
  59. */
  60. struct aw9523_irq {
  61. struct mutex lock;
  62. struct irq_chip *irqchip;
  63. u16 cached_gpio;
  64. };
  65. /*
  66. * struct aw9523_pinmux - Pin mux params
  67. * @name: Name of the mux
  68. * @grps: Groups of the mux
  69. * @num_grps: Number of groups (sizeof array grps)
  70. */
  71. struct aw9523_pinmux {
  72. const char *name;
  73. const char * const *grps;
  74. const u8 num_grps;
  75. };
  76. /*
  77. * struct aw9523 - Main driver structure
  78. * @dev: device handle
  79. * @regmap: regmap handle for current device
  80. * @i2c_lock: Mutex lock for i2c operations
  81. * @reset_gpio: Hardware reset (RSTN) signal GPIO
  82. * @vio_vreg: VCC regulator (Optional)
  83. * @pctl: pinctrl handle for current device
  84. * @gpio: structure holding gpiochip params
  85. * @irq: Interrupt controller structure
  86. */
  87. struct aw9523 {
  88. struct device *dev;
  89. struct regmap *regmap;
  90. struct mutex i2c_lock;
  91. struct gpio_desc *reset_gpio;
  92. struct regulator *vio_vreg;
  93. struct pinctrl_dev *pctl;
  94. struct gpio_chip gpio;
  95. struct aw9523_irq *irq;
  96. };
  97. static const struct pinctrl_pin_desc aw9523_pins[] = {
  98. /* Port 0 */
  99. PINCTRL_PIN(0, "gpio0"),
  100. PINCTRL_PIN(1, "gpio1"),
  101. PINCTRL_PIN(2, "gpio2"),
  102. PINCTRL_PIN(3, "gpio3"),
  103. PINCTRL_PIN(4, "gpio4"),
  104. PINCTRL_PIN(5, "gpio5"),
  105. PINCTRL_PIN(6, "gpio6"),
  106. PINCTRL_PIN(7, "gpio7"),
  107. /* Port 1 */
  108. PINCTRL_PIN(8, "gpio8"),
  109. PINCTRL_PIN(9, "gpio9"),
  110. PINCTRL_PIN(10, "gpio10"),
  111. PINCTRL_PIN(11, "gpio11"),
  112. PINCTRL_PIN(12, "gpio12"),
  113. PINCTRL_PIN(13, "gpio13"),
  114. PINCTRL_PIN(14, "gpio14"),
  115. PINCTRL_PIN(15, "gpio15"),
  116. };
  117. static int aw9523_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  118. {
  119. return ARRAY_SIZE(aw9523_pins);
  120. }
  121. static const char *aw9523_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  122. unsigned int selector)
  123. {
  124. return aw9523_pins[selector].name;
  125. }
  126. static int aw9523_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  127. unsigned int selector,
  128. const unsigned int **pins,
  129. unsigned int *num_pins)
  130. {
  131. *pins = &aw9523_pins[selector].number;
  132. *num_pins = 1;
  133. return 0;
  134. }
  135. static const struct pinctrl_ops aw9523_pinctrl_ops = {
  136. .get_groups_count = aw9523_pinctrl_get_groups_count,
  137. .get_group_pins = aw9523_pinctrl_get_group_pins,
  138. .get_group_name = aw9523_pinctrl_get_group_name,
  139. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  140. .dt_free_map = pinconf_generic_dt_free_map,
  141. };
  142. static const char * const gpio_pwm_groups[] = {
  143. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5",
  144. "gpio6", "gpio7", "gpio8", "gpio9", "gpio10", "gpio11",
  145. "gpio12", "gpio13", "gpio14", "gpio15"
  146. };
  147. /* Warning: Do NOT reorder this array */
  148. static const struct aw9523_pinmux aw9523_pmx[] = {
  149. {
  150. .name = "pwm",
  151. .grps = gpio_pwm_groups,
  152. .num_grps = ARRAY_SIZE(gpio_pwm_groups),
  153. },
  154. {
  155. .name = "gpio",
  156. .grps = gpio_pwm_groups,
  157. .num_grps = ARRAY_SIZE(gpio_pwm_groups),
  158. },
  159. };
  160. static int aw9523_pmx_get_funcs_count(struct pinctrl_dev *pctl)
  161. {
  162. return ARRAY_SIZE(aw9523_pmx);
  163. }
  164. static const char *aw9523_pmx_get_fname(struct pinctrl_dev *pctl,
  165. unsigned int sel)
  166. {
  167. return aw9523_pmx[sel].name;
  168. }
  169. static int aw9523_pmx_get_groups(struct pinctrl_dev *pctl, unsigned int sel,
  170. const char * const **groups,
  171. unsigned int * const num_groups)
  172. {
  173. *groups = aw9523_pmx[sel].grps;
  174. *num_groups = aw9523_pmx[sel].num_grps;
  175. return 0;
  176. }
  177. static int aw9523_pmx_set_mux(struct pinctrl_dev *pctl, unsigned int fsel,
  178. unsigned int grp)
  179. {
  180. struct aw9523 *awi = pinctrl_dev_get_drvdata(pctl);
  181. int ret, pin = aw9523_pins[grp].number % AW9523_PINS_PER_PORT;
  182. if (fsel >= ARRAY_SIZE(aw9523_pmx))
  183. return -EINVAL;
  184. /*
  185. * This maps directly to the aw9523_pmx array: programming a
  186. * high bit means "gpio" and a low bit means "pwm".
  187. */
  188. mutex_lock(&awi->i2c_lock);
  189. ret = regmap_update_bits(awi->regmap, AW9523_REG_PORT_MODE(pin),
  190. BIT(pin), (fsel ? BIT(pin) : 0));
  191. mutex_unlock(&awi->i2c_lock);
  192. return ret;
  193. }
  194. static const struct pinmux_ops aw9523_pinmux_ops = {
  195. .get_functions_count = aw9523_pmx_get_funcs_count,
  196. .get_function_name = aw9523_pmx_get_fname,
  197. .get_function_groups = aw9523_pmx_get_groups,
  198. .set_mux = aw9523_pmx_set_mux,
  199. };
  200. static int aw9523_pcfg_param_to_reg(enum pin_config_param pcp, int pin, u8 *r)
  201. {
  202. u8 reg;
  203. switch (pcp) {
  204. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  205. case PIN_CONFIG_BIAS_PULL_DOWN:
  206. case PIN_CONFIG_BIAS_PULL_UP:
  207. reg = AW9523_REG_IN_STATE(pin);
  208. break;
  209. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  210. case PIN_CONFIG_DRIVE_PUSH_PULL:
  211. reg = AW9523_REG_GCR;
  212. break;
  213. case PIN_CONFIG_INPUT_ENABLE:
  214. case PIN_CONFIG_OUTPUT_ENABLE:
  215. reg = AW9523_REG_CONF_STATE(pin);
  216. break;
  217. case PIN_CONFIG_OUTPUT:
  218. reg = AW9523_REG_OUT_STATE(pin);
  219. break;
  220. default:
  221. return -ENOTSUPP;
  222. }
  223. *r = reg;
  224. return 0;
  225. }
  226. static int aw9523_pconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  227. unsigned long *config)
  228. {
  229. struct aw9523 *awi = pinctrl_dev_get_drvdata(pctldev);
  230. enum pin_config_param param = pinconf_to_config_param(*config);
  231. int regbit = pin % AW9523_PINS_PER_PORT;
  232. unsigned int val;
  233. u8 reg;
  234. int rc;
  235. rc = aw9523_pcfg_param_to_reg(param, pin, &reg);
  236. if (rc)
  237. return rc;
  238. mutex_lock(&awi->i2c_lock);
  239. rc = regmap_read(awi->regmap, reg, &val);
  240. mutex_unlock(&awi->i2c_lock);
  241. if (rc)
  242. return rc;
  243. switch (param) {
  244. case PIN_CONFIG_BIAS_PULL_UP:
  245. case PIN_CONFIG_INPUT_ENABLE:
  246. case PIN_CONFIG_OUTPUT:
  247. val &= BIT(regbit);
  248. break;
  249. case PIN_CONFIG_BIAS_PULL_DOWN:
  250. case PIN_CONFIG_OUTPUT_ENABLE:
  251. val &= BIT(regbit);
  252. val = !val;
  253. break;
  254. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  255. if (pin >= AW9523_PINS_PER_PORT)
  256. val = 0;
  257. else
  258. val = !FIELD_GET(AW9523_GCR_GPOMD_MASK, val);
  259. break;
  260. case PIN_CONFIG_DRIVE_PUSH_PULL:
  261. if (pin >= AW9523_PINS_PER_PORT)
  262. val = 1;
  263. else
  264. val = FIELD_GET(AW9523_GCR_GPOMD_MASK, val);
  265. break;
  266. default:
  267. return -ENOTSUPP;
  268. }
  269. if (val < 1)
  270. return -EINVAL;
  271. *config = pinconf_to_config_packed(param, !!val);
  272. return rc;
  273. }
  274. static int aw9523_pconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  275. unsigned long *configs, unsigned int num_configs)
  276. {
  277. struct aw9523 *awi = pinctrl_dev_get_drvdata(pctldev);
  278. enum pin_config_param param;
  279. int regbit = pin % AW9523_PINS_PER_PORT;
  280. u32 arg;
  281. u8 reg;
  282. unsigned int mask, val;
  283. int i, rc;
  284. mutex_lock(&awi->i2c_lock);
  285. for (i = 0; i < num_configs; i++) {
  286. param = pinconf_to_config_param(configs[i]);
  287. arg = pinconf_to_config_argument(configs[i]);
  288. rc = aw9523_pcfg_param_to_reg(param, pin, &reg);
  289. if (rc)
  290. goto end;
  291. switch (param) {
  292. case PIN_CONFIG_OUTPUT:
  293. /* First, enable pin output */
  294. rc = regmap_update_bits(awi->regmap,
  295. AW9523_REG_CONF_STATE(pin),
  296. BIT(regbit), 0);
  297. if (rc)
  298. goto end;
  299. /* Then, fall through to config output level */
  300. fallthrough;
  301. case PIN_CONFIG_OUTPUT_ENABLE:
  302. arg = !arg;
  303. fallthrough;
  304. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  305. case PIN_CONFIG_BIAS_PULL_DOWN:
  306. case PIN_CONFIG_BIAS_PULL_UP:
  307. case PIN_CONFIG_INPUT_ENABLE:
  308. mask = BIT(regbit);
  309. val = arg ? BIT(regbit) : 0;
  310. break;
  311. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  312. /* Open-Drain is supported only on port 0 */
  313. if (pin >= AW9523_PINS_PER_PORT) {
  314. rc = -ENOTSUPP;
  315. goto end;
  316. }
  317. mask = AW9523_GCR_GPOMD_MASK;
  318. val = 0;
  319. break;
  320. case PIN_CONFIG_DRIVE_PUSH_PULL:
  321. /* Port 1 is always Push-Pull */
  322. if (pin >= AW9523_PINS_PER_PORT) {
  323. mask = 0;
  324. val = 0;
  325. continue;
  326. }
  327. mask = AW9523_GCR_GPOMD_MASK;
  328. val = AW9523_GCR_GPOMD_MASK;
  329. break;
  330. default:
  331. rc = -ENOTSUPP;
  332. goto end;
  333. }
  334. rc = regmap_update_bits(awi->regmap, reg, mask, val);
  335. if (rc)
  336. goto end;
  337. }
  338. end:
  339. mutex_unlock(&awi->i2c_lock);
  340. return rc;
  341. }
  342. static const struct pinconf_ops aw9523_pinconf_ops = {
  343. .pin_config_get = aw9523_pconf_get,
  344. .pin_config_set = aw9523_pconf_set,
  345. .is_generic = true,
  346. };
  347. /*
  348. * aw9523_get_pin_direction - Get pin direction
  349. * @regmap: Regmap structure
  350. * @pin: gpiolib pin number
  351. * @n: pin index in port register
  352. *
  353. * Return: Pin direction for success or negative number for error
  354. */
  355. static int aw9523_get_pin_direction(struct regmap *regmap, u8 pin, u8 n)
  356. {
  357. int val, ret;
  358. ret = regmap_read(regmap, AW9523_REG_CONF_STATE(pin), &val);
  359. if (ret < 0)
  360. return ret;
  361. return (val & BIT(n)) == BIT(n);
  362. }
  363. /*
  364. * aw9523_get_port_state - Get input or output state for entire port
  365. * @regmap: Regmap structure
  366. * @pin: gpiolib pin number
  367. * @regbit: hw pin index, used to retrieve port number
  368. * @state: returned port state
  369. *
  370. * Return: Zero for success or negative number for error
  371. */
  372. static int aw9523_get_port_state(struct regmap *regmap, u8 pin,
  373. u8 regbit, unsigned int *state)
  374. {
  375. u8 reg;
  376. int dir;
  377. dir = aw9523_get_pin_direction(regmap, pin, regbit);
  378. if (dir < 0)
  379. return dir;
  380. if (dir == GPIO_LINE_DIRECTION_IN)
  381. reg = AW9523_REG_IN_STATE(pin);
  382. else
  383. reg = AW9523_REG_OUT_STATE(pin);
  384. return regmap_read(regmap, reg, state);
  385. }
  386. static int aw9523_gpio_irq_type(struct irq_data *d, unsigned int type)
  387. {
  388. switch (type) {
  389. case IRQ_TYPE_NONE:
  390. case IRQ_TYPE_EDGE_BOTH:
  391. return 0;
  392. default:
  393. return -EINVAL;
  394. };
  395. }
  396. /*
  397. * aw9523_irq_mask - Mask interrupt
  398. * @d: irq data
  399. *
  400. * Sets which interrupt to mask in the bitmap;
  401. * The interrupt will be masked when unlocking the irq bus.
  402. */
  403. static void aw9523_irq_mask(struct irq_data *d)
  404. {
  405. struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  406. unsigned int n = d->hwirq % AW9523_PINS_PER_PORT;
  407. regmap_update_bits(awi->regmap,
  408. AW9523_REG_INTR_DIS(d->hwirq),
  409. BIT(n), BIT(n));
  410. }
  411. /*
  412. * aw9523_irq_unmask - Unmask interrupt
  413. * @d: irq data
  414. *
  415. * Sets which interrupt to unmask in the bitmap;
  416. * The interrupt will be masked when unlocking the irq bus.
  417. */
  418. static void aw9523_irq_unmask(struct irq_data *d)
  419. {
  420. struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  421. unsigned int n = d->hwirq % AW9523_PINS_PER_PORT;
  422. regmap_update_bits(awi->regmap,
  423. AW9523_REG_INTR_DIS(d->hwirq),
  424. BIT(n), 0);
  425. }
  426. static irqreturn_t aw9523_irq_thread_func(int irq, void *dev_id)
  427. {
  428. struct aw9523 *awi = (struct aw9523 *)dev_id;
  429. unsigned long n, val = 0;
  430. unsigned long changed_gpio;
  431. unsigned int tmp, port_pin, i, ret;
  432. for (i = 0; i < AW9523_NUM_PORTS; i++) {
  433. port_pin = i * AW9523_PINS_PER_PORT;
  434. ret = regmap_read(awi->regmap,
  435. AW9523_REG_IN_STATE(port_pin),
  436. &tmp);
  437. if (ret)
  438. return ret;
  439. val |= (u8)tmp << (i * 8);
  440. }
  441. /* Handle GPIO input release interrupt as well */
  442. changed_gpio = awi->irq->cached_gpio ^ val;
  443. awi->irq->cached_gpio = val;
  444. /*
  445. * To avoid up to four *slow* i2c reads from any driver hooked
  446. * up to our interrupts, just check for the irq_find_mapping
  447. * result: if the interrupt is not mapped, then we don't want
  448. * to care about it.
  449. */
  450. for_each_set_bit(n, &changed_gpio, awi->gpio.ngpio) {
  451. tmp = irq_find_mapping(awi->gpio.irq.domain, n);
  452. if (tmp <= 0)
  453. continue;
  454. handle_nested_irq(tmp);
  455. }
  456. return IRQ_HANDLED;
  457. }
  458. /*
  459. * aw9523_irq_bus_lock - Grab lock for interrupt operation
  460. * @d: irq data
  461. */
  462. static void aw9523_irq_bus_lock(struct irq_data *d)
  463. {
  464. struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  465. mutex_lock(&awi->irq->lock);
  466. regcache_cache_only(awi->regmap, true);
  467. }
  468. /*
  469. * aw9523_irq_bus_sync_unlock - Synchronize state and unlock
  470. * @d: irq data
  471. *
  472. * Writes the interrupt mask bits (found in the bit map) to the
  473. * hardware, then unlocks the bus.
  474. */
  475. static void aw9523_irq_bus_sync_unlock(struct irq_data *d)
  476. {
  477. struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
  478. regcache_cache_only(awi->regmap, false);
  479. regcache_sync(awi->regmap);
  480. mutex_unlock(&awi->irq->lock);
  481. }
  482. static int aw9523_gpio_get_direction(struct gpio_chip *chip,
  483. unsigned int offset)
  484. {
  485. struct aw9523 *awi = gpiochip_get_data(chip);
  486. u8 regbit = offset % AW9523_PINS_PER_PORT;
  487. int ret;
  488. mutex_lock(&awi->i2c_lock);
  489. ret = aw9523_get_pin_direction(awi->regmap, offset, regbit);
  490. mutex_unlock(&awi->i2c_lock);
  491. return ret;
  492. }
  493. static int aw9523_gpio_get(struct gpio_chip *chip, unsigned int offset)
  494. {
  495. struct aw9523 *awi = gpiochip_get_data(chip);
  496. u8 regbit = offset % AW9523_PINS_PER_PORT;
  497. unsigned int val;
  498. int ret;
  499. mutex_lock(&awi->i2c_lock);
  500. ret = aw9523_get_port_state(awi->regmap, offset, regbit, &val);
  501. mutex_unlock(&awi->i2c_lock);
  502. if (ret)
  503. return ret;
  504. return !!(val & BIT(regbit));
  505. }
  506. /**
  507. * _aw9523_gpio_get_multiple - Get I/O state for an entire port
  508. * @regmap: Regmap structure
  509. * @pin: gpiolib pin number
  510. * @regbit: hw pin index, used to retrieve port number
  511. * @state: returned port I/O state
  512. *
  513. * Return: Zero for success or negative number for error
  514. */
  515. static int _aw9523_gpio_get_multiple(struct aw9523 *awi, u8 regbit,
  516. u8 *state, u8 mask)
  517. {
  518. u32 dir_in, val;
  519. u8 m;
  520. int ret;
  521. /* Registers are 8-bits wide */
  522. ret = regmap_read(awi->regmap, AW9523_REG_CONF_STATE(regbit), &dir_in);
  523. if (ret)
  524. return ret;
  525. *state = 0;
  526. m = mask & dir_in;
  527. if (m) {
  528. ret = regmap_read(awi->regmap, AW9523_REG_IN_STATE(regbit),
  529. &val);
  530. if (ret)
  531. return ret;
  532. *state |= (u8)val & m;
  533. }
  534. m = mask & ~dir_in;
  535. if (m) {
  536. ret = regmap_read(awi->regmap, AW9523_REG_OUT_STATE(regbit),
  537. &val);
  538. if (ret)
  539. return ret;
  540. *state |= (u8)val & m;
  541. }
  542. return 0;
  543. }
  544. static int aw9523_gpio_get_multiple(struct gpio_chip *chip,
  545. unsigned long *mask,
  546. unsigned long *bits)
  547. {
  548. struct aw9523 *awi = gpiochip_get_data(chip);
  549. u8 m, state = 0;
  550. int ret;
  551. mutex_lock(&awi->i2c_lock);
  552. /* Port 0 (gpio 0-7) */
  553. m = *mask & U8_MAX;
  554. if (m) {
  555. ret = _aw9523_gpio_get_multiple(awi, 0, &state, m);
  556. if (ret)
  557. goto out;
  558. }
  559. *bits = state;
  560. /* Port 1 (gpio 8-15) */
  561. m = (*mask >> 8) & U8_MAX;
  562. if (m) {
  563. ret = _aw9523_gpio_get_multiple(awi, AW9523_PINS_PER_PORT,
  564. &state, m);
  565. if (ret)
  566. goto out;
  567. *bits |= (state << 8);
  568. }
  569. out:
  570. mutex_unlock(&awi->i2c_lock);
  571. return ret;
  572. }
  573. static void aw9523_gpio_set_multiple(struct gpio_chip *chip,
  574. unsigned long *mask,
  575. unsigned long *bits)
  576. {
  577. struct aw9523 *awi = gpiochip_get_data(chip);
  578. u8 mask_lo, mask_hi, bits_lo, bits_hi;
  579. unsigned int reg;
  580. int ret = 0;
  581. mask_lo = *mask & U8_MAX;
  582. mask_hi = (*mask >> 8) & U8_MAX;
  583. mutex_lock(&awi->i2c_lock);
  584. if (mask_hi) {
  585. reg = AW9523_REG_OUT_STATE(AW9523_PINS_PER_PORT);
  586. bits_hi = (*bits >> 8) & U8_MAX;
  587. ret = regmap_write_bits(awi->regmap, reg, mask_hi, bits_hi);
  588. if (ret) {
  589. dev_warn(awi->dev, "Cannot write port1 out level\n");
  590. goto out;
  591. }
  592. }
  593. if (mask_lo) {
  594. reg = AW9523_REG_OUT_STATE(0);
  595. bits_lo = *bits & U8_MAX;
  596. ret = regmap_write_bits(awi->regmap, reg, mask_lo, bits_lo);
  597. if (ret)
  598. dev_warn(awi->dev, "Cannot write port0 out level\n");
  599. }
  600. out:
  601. mutex_unlock(&awi->i2c_lock);
  602. }
  603. static void aw9523_gpio_set(struct gpio_chip *chip,
  604. unsigned int offset, int value)
  605. {
  606. struct aw9523 *awi = gpiochip_get_data(chip);
  607. u8 regbit = offset % AW9523_PINS_PER_PORT;
  608. mutex_lock(&awi->i2c_lock);
  609. regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset),
  610. BIT(regbit), value ? BIT(regbit) : 0);
  611. mutex_unlock(&awi->i2c_lock);
  612. }
  613. static int aw9523_direction_input(struct gpio_chip *chip, unsigned int offset)
  614. {
  615. struct aw9523 *awi = gpiochip_get_data(chip);
  616. u8 regbit = offset % AW9523_PINS_PER_PORT;
  617. int ret;
  618. mutex_lock(&awi->i2c_lock);
  619. ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset),
  620. BIT(regbit), BIT(regbit));
  621. mutex_unlock(&awi->i2c_lock);
  622. return ret;
  623. }
  624. static int aw9523_direction_output(struct gpio_chip *chip,
  625. unsigned int offset, int value)
  626. {
  627. struct aw9523 *awi = gpiochip_get_data(chip);
  628. u8 regbit = offset % AW9523_PINS_PER_PORT;
  629. int ret;
  630. mutex_lock(&awi->i2c_lock);
  631. ret = regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset),
  632. BIT(regbit), value ? BIT(regbit) : 0);
  633. if (ret)
  634. goto end;
  635. ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset),
  636. BIT(regbit), 0);
  637. end:
  638. mutex_unlock(&awi->i2c_lock);
  639. return ret;
  640. }
  641. static int aw9523_drive_reset_gpio(struct aw9523 *awi)
  642. {
  643. unsigned int chip_id;
  644. int ret;
  645. /*
  646. * If the chip is already configured for any reason, then we
  647. * will probably succeed in sending the soft reset signal to
  648. * the hardware through I2C: this operation takes less time
  649. * compared to a full HW reset and it gives the same results.
  650. */
  651. ret = regmap_write(awi->regmap, AW9523_REG_SOFT_RESET, 0);
  652. if (ret == 0)
  653. goto done;
  654. dev_dbg(awi->dev, "Cannot execute soft reset: trying hard reset\n");
  655. ret = gpiod_direction_output(awi->reset_gpio, 0);
  656. if (ret)
  657. return ret;
  658. /* The reset pulse has to be longer than 20uS due to deglitch */
  659. usleep_range(AW9523_HW_RESET_US, AW9523_HW_RESET_US + 1);
  660. ret = gpiod_direction_output(awi->reset_gpio, 1);
  661. if (ret)
  662. return ret;
  663. done:
  664. /* The HW needs at least 1uS to reliably recover after reset */
  665. usleep_range(AW9523_HW_RESET_RECOVERY_US,
  666. AW9523_HW_RESET_RECOVERY_US + 1);
  667. /* Check the ChipID */
  668. ret = regmap_read(awi->regmap, AW9523_REG_CHIPID, &chip_id);
  669. if (ret) {
  670. dev_err(awi->dev, "Cannot read Chip ID: %d\n", ret);
  671. return ret;
  672. }
  673. if (chip_id != AW9523_VAL_EXPECTED_CHIPID) {
  674. dev_err(awi->dev, "Bad ChipID; read 0x%x, expected 0x%x\n",
  675. chip_id, AW9523_VAL_EXPECTED_CHIPID);
  676. return -EINVAL;
  677. }
  678. return 0;
  679. }
  680. static int aw9523_hw_reset(struct aw9523 *awi)
  681. {
  682. int ret, max_retries = 2;
  683. /* Sometimes the chip needs more than one reset cycle */
  684. do {
  685. ret = aw9523_drive_reset_gpio(awi);
  686. if (ret == 0)
  687. break;
  688. max_retries--;
  689. } while (max_retries);
  690. return ret;
  691. }
  692. static int aw9523_init_gpiochip(struct aw9523 *awi, unsigned int npins)
  693. {
  694. struct device *dev = awi->dev;
  695. struct gpio_chip *gpiochip = &awi->gpio;
  696. gpiochip->label = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
  697. if (!gpiochip->label)
  698. return -ENOMEM;
  699. gpiochip->base = -1;
  700. gpiochip->ngpio = npins;
  701. gpiochip->get_direction = aw9523_gpio_get_direction;
  702. gpiochip->direction_input = aw9523_direction_input;
  703. gpiochip->direction_output = aw9523_direction_output;
  704. gpiochip->get = aw9523_gpio_get;
  705. gpiochip->get_multiple = aw9523_gpio_get_multiple;
  706. gpiochip->set = aw9523_gpio_set;
  707. gpiochip->set_multiple = aw9523_gpio_set_multiple;
  708. gpiochip->set_config = gpiochip_generic_config;
  709. gpiochip->parent = dev;
  710. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
  711. gpiochip->fwnode = dev->fwnode;
  712. #else
  713. gpiochip->of_node = dev->of_node;
  714. #endif
  715. gpiochip->owner = THIS_MODULE;
  716. gpiochip->can_sleep = true;
  717. return 0;
  718. }
  719. static int aw9523_init_irq(struct aw9523 *awi, int irq)
  720. {
  721. struct device *dev = awi->dev;
  722. struct gpio_irq_chip *gpioirq;
  723. struct irq_chip *irqchip;
  724. int ret;
  725. if (!device_property_read_bool(dev, "interrupt-controller"))
  726. return 0;
  727. irqchip = devm_kzalloc(dev, sizeof(*irqchip), GFP_KERNEL);
  728. if (!irqchip)
  729. return -ENOMEM;
  730. awi->irq = devm_kzalloc(dev, sizeof(*awi->irq), GFP_KERNEL);
  731. if (!awi->irq)
  732. return -ENOMEM;
  733. irqchip->name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
  734. if (!irqchip->name)
  735. return -ENOMEM;
  736. irqchip->irq_mask = aw9523_irq_mask;
  737. irqchip->irq_unmask = aw9523_irq_unmask;
  738. irqchip->irq_bus_lock = aw9523_irq_bus_lock;
  739. irqchip->irq_bus_sync_unlock = aw9523_irq_bus_sync_unlock;
  740. irqchip->irq_set_type = aw9523_gpio_irq_type;
  741. awi->irq->irqchip = irqchip;
  742. mutex_init(&awi->irq->lock);
  743. ret = devm_request_threaded_irq(dev, irq, NULL, aw9523_irq_thread_func,
  744. IRQF_ONESHOT, dev_name(dev), awi);
  745. if (ret) {
  746. dev_err(dev, "Failed to request irq %d\n", irq);
  747. return ret;
  748. }
  749. gpioirq = &awi->gpio.irq;
  750. gpioirq->chip = irqchip;
  751. gpioirq->parent_handler = NULL;
  752. gpioirq->num_parents = 0;
  753. gpioirq->parents = NULL;
  754. gpioirq->default_type = IRQ_TYPE_LEVEL_MASK;
  755. gpioirq->handler = handle_simple_irq;
  756. gpioirq->threaded = true;
  757. gpioirq->first = 0;
  758. return 0;
  759. }
  760. static bool aw9523_is_reg_hole(unsigned int reg)
  761. {
  762. return (reg > AW9523_REG_PORT_MODE(AW9523_PINS_PER_PORT) &&
  763. reg < AW9523_REG_SOFT_RESET) ||
  764. (reg > AW9523_REG_INTR_DIS(AW9523_PINS_PER_PORT) &&
  765. reg < AW9523_REG_CHIPID);
  766. }
  767. static bool aw9523_readable_reg(struct device *dev, unsigned int reg)
  768. {
  769. /* All available registers (minus holes) can be read */
  770. return !aw9523_is_reg_hole(reg);
  771. }
  772. static bool aw9523_volatile_reg(struct device *dev, unsigned int reg)
  773. {
  774. return aw9523_is_reg_hole(reg) ||
  775. reg == AW9523_REG_IN_STATE(0) ||
  776. reg == AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT) ||
  777. reg == AW9523_REG_CHIPID ||
  778. reg == AW9523_REG_SOFT_RESET;
  779. }
  780. static bool aw9523_writeable_reg(struct device *dev, unsigned int reg)
  781. {
  782. return !aw9523_is_reg_hole(reg) && reg != AW9523_REG_CHIPID;
  783. }
  784. static bool aw9523_precious_reg(struct device *dev, unsigned int reg)
  785. {
  786. /* Reading AW9523_REG_IN_STATE clears interrupt status */
  787. return aw9523_is_reg_hole(reg) ||
  788. reg == AW9523_REG_IN_STATE(0) ||
  789. reg == AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT);
  790. }
  791. static const struct regmap_config aw9523_regmap = {
  792. .reg_bits = 8,
  793. .val_bits = 8,
  794. .reg_stride = 1,
  795. .precious_reg = aw9523_precious_reg,
  796. .readable_reg = aw9523_readable_reg,
  797. .volatile_reg = aw9523_volatile_reg,
  798. .writeable_reg = aw9523_writeable_reg,
  799. .cache_type = REGCACHE_FLAT,
  800. .disable_locking = true,
  801. .num_reg_defaults_raw = AW9523_REG_SOFT_RESET,
  802. };
  803. static int aw9523_hw_init(struct aw9523 *awi)
  804. {
  805. u8 p1_pin = AW9523_PINS_PER_PORT;
  806. unsigned int val;
  807. int ret;
  808. /* No register caching during initialization */
  809. regcache_cache_bypass(awi->regmap, true);
  810. /* Bring up the chip */
  811. ret = aw9523_hw_reset(awi);
  812. if (ret) {
  813. dev_err(awi->dev, "HW Reset failed: %d\n", ret);
  814. return ret;
  815. }
  816. /*
  817. * This is the expected chip and it is running: it's time to
  818. * set a safe default configuration in case the user doesn't
  819. * configure (all of the available) pins in this chip.
  820. * P.S.: The writes order doesn't matter.
  821. */
  822. /* Set all pins as GPIO */
  823. ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(0), U8_MAX);
  824. if (ret)
  825. return ret;
  826. ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(p1_pin), U8_MAX);
  827. if (ret)
  828. return ret;
  829. /* Set Open-Drain mode on Port 0 (Port 1 is always P-P) */
  830. ret = regmap_write(awi->regmap, AW9523_REG_GCR, 0);
  831. if (ret)
  832. return ret;
  833. /* Set all pins as inputs */
  834. ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(0), U8_MAX);
  835. if (ret)
  836. return ret;
  837. ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(p1_pin), U8_MAX);
  838. if (ret)
  839. return ret;
  840. /* Disable all interrupts to avoid unreasoned wakeups */
  841. ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(0), U8_MAX);
  842. if (ret)
  843. return ret;
  844. ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(p1_pin), U8_MAX);
  845. if (ret)
  846. return ret;
  847. /* Clear setup-generated interrupts by performing a port state read */
  848. ret = aw9523_get_port_state(awi->regmap, 0, 0, &val);
  849. if (ret)
  850. return ret;
  851. ret = aw9523_get_port_state(awi->regmap, p1_pin, 0, &val);
  852. if (ret)
  853. return ret;
  854. /* Everything went fine: activate and reinitialize register cache */
  855. regcache_cache_bypass(awi->regmap, false);
  856. return regmap_reinit_cache(awi->regmap, &aw9523_regmap);
  857. }
  858. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
  859. static int aw9523_probe(struct i2c_client *client)
  860. #else
  861. static int aw9523_probe(struct i2c_client *client,
  862. const struct i2c_device_id *id)
  863. #endif
  864. {
  865. struct device *dev = &client->dev;
  866. struct pinctrl_desc *pdesc;
  867. struct aw9523 *awi;
  868. int ret;
  869. awi = devm_kzalloc(dev, sizeof(*awi), GFP_KERNEL);
  870. if (!awi)
  871. return -ENOMEM;
  872. i2c_set_clientdata(client, awi);
  873. awi->dev = dev;
  874. awi->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  875. if (IS_ERR(awi->reset_gpio))
  876. return PTR_ERR(awi->reset_gpio);
  877. gpiod_set_consumer_name(awi->reset_gpio, "aw9523 reset");
  878. awi->regmap = devm_regmap_init_i2c(client, &aw9523_regmap);
  879. if (IS_ERR(awi->regmap))
  880. return PTR_ERR(awi->regmap);
  881. awi->vio_vreg = devm_regulator_get_optional(dev, "vio");
  882. if (IS_ERR(awi->vio_vreg)) {
  883. if (PTR_ERR(awi->vio_vreg) == -EPROBE_DEFER)
  884. return -EPROBE_DEFER;
  885. awi->vio_vreg = NULL;
  886. } else {
  887. ret = regulator_enable(awi->vio_vreg);
  888. if (ret)
  889. return ret;
  890. }
  891. mutex_init(&awi->i2c_lock);
  892. lockdep_set_subclass(&awi->i2c_lock,
  893. i2c_adapter_depth(client->adapter));
  894. pdesc = devm_kzalloc(dev, sizeof(*pdesc), GFP_KERNEL);
  895. if (!pdesc)
  896. return -ENOMEM;
  897. ret = aw9523_hw_init(awi);
  898. if (ret)
  899. goto err_disable_vregs;
  900. pdesc->name = dev_name(dev);
  901. pdesc->owner = THIS_MODULE;
  902. pdesc->pctlops = &aw9523_pinctrl_ops;
  903. pdesc->pmxops = &aw9523_pinmux_ops;
  904. pdesc->confops = &aw9523_pinconf_ops;
  905. pdesc->pins = aw9523_pins;
  906. pdesc->npins = ARRAY_SIZE(aw9523_pins);
  907. ret = aw9523_init_gpiochip(awi, pdesc->npins);
  908. if (ret)
  909. goto err_disable_vregs;
  910. if (client->irq) {
  911. ret = aw9523_init_irq(awi, client->irq);
  912. if (ret)
  913. goto err_disable_vregs;
  914. }
  915. awi->pctl = devm_pinctrl_register(dev, pdesc, awi);
  916. if (IS_ERR(awi->pctl)) {
  917. ret = PTR_ERR(awi->pctl);
  918. dev_err(dev, "Cannot register pinctrl: %d", ret);
  919. goto err_disable_vregs;
  920. }
  921. ret = devm_gpiochip_add_data(dev, &awi->gpio, awi);
  922. if (ret)
  923. goto err_disable_vregs;
  924. return ret;
  925. err_disable_vregs:
  926. if (awi->vio_vreg)
  927. regulator_disable(awi->vio_vreg);
  928. mutex_destroy(&awi->i2c_lock);
  929. return ret;
  930. }
  931. static int aw9523_remove(struct i2c_client *client)
  932. {
  933. struct aw9523 *awi = i2c_get_clientdata(client);
  934. int ret;
  935. if (!awi)
  936. return 0;
  937. /*
  938. * If the chip VIO is connected to a regulator that we can turn
  939. * off, life is easy... otherwise, reinitialize the chip and
  940. * set the pins to hardware defaults before removing the driver
  941. * to leave it in a clean, safe and predictable state.
  942. */
  943. if (awi->vio_vreg) {
  944. regulator_disable(awi->vio_vreg);
  945. } else {
  946. mutex_lock(&awi->i2c_lock);
  947. ret = aw9523_hw_init(awi);
  948. mutex_unlock(&awi->i2c_lock);
  949. if (ret)
  950. return ret;
  951. }
  952. mutex_destroy(&awi->i2c_lock);
  953. return 0;
  954. }
  955. static void aw9523_remove_void(struct i2c_client *client)
  956. {
  957. aw9523_remove(client);
  958. }
  959. static const struct i2c_device_id aw9523_i2c_id_table[] = {
  960. { "aw9523_i2c", 0 },
  961. { }
  962. };
  963. MODULE_DEVICE_TABLE(i2c, aw9523_i2c_id_table);
  964. static const struct of_device_id of_aw9523_i2c_match[] = {
  965. { .compatible = "awinic,aw9523-pinctrl", },
  966. };
  967. MODULE_DEVICE_TABLE(of, of_aw9523_i2c_match);
  968. static struct i2c_driver aw9523_driver = {
  969. .driver = {
  970. .name = "aw9523-pinctrl",
  971. .of_match_table = of_aw9523_i2c_match,
  972. },
  973. .probe = aw9523_probe,
  974. .remove = aw9523_remove_void,
  975. .id_table = aw9523_i2c_id_table,
  976. };
  977. module_i2c_driver(aw9523_driver);
  978. MODULE_DESCRIPTION("Awinic AW9523 I2C GPIO Expander driver");
  979. MODULE_AUTHOR("AngeloGioacchino Del Regno <[email protected]>");
  980. MODULE_LICENSE("GPL v2");
  981. MODULE_ALIAS("platform:aw9523");