805-01-v6.9-pinctrl-Add-driver-for-Awinic-AW9523-B-I2C-GPIO-Expa.patch 31 KB

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