gpio-pca953x.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * PCA953x 4/8/16/24/40 bit I/O ports
  3. *
  4. * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  5. * Copyright (C) 2007 Marvell International Ltd.
  6. *
  7. * Derived from drivers/i2c/chips/pca9539.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_data/pca953x.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <asm/unaligned.h>
  25. #define PCA953X_INPUT 0x00
  26. #define PCA953X_OUTPUT 0x01
  27. #define PCA953X_INVERT 0x02
  28. #define PCA953X_DIRECTION 0x03
  29. #define REG_ADDR_AI 0x80
  30. #define PCA957X_IN 0x00
  31. #define PCA957X_INVRT 0x01
  32. #define PCA957X_BKEN 0x02
  33. #define PCA957X_PUPD 0x03
  34. #define PCA957X_CFG 0x04
  35. #define PCA957X_OUT 0x05
  36. #define PCA957X_MSK 0x06
  37. #define PCA957X_INTS 0x07
  38. #define PCAL953X_OUT_STRENGTH 0x20
  39. #define PCAL953X_IN_LATCH 0x22
  40. #define PCAL953X_PULL_EN 0x23
  41. #define PCAL953X_PULL_SEL 0x24
  42. #define PCAL953X_INT_MASK 0x25
  43. #define PCAL953X_INT_STAT 0x26
  44. #define PCAL953X_OUT_CONF 0x27
  45. #define PCAL6524_INT_EDGE 0x28
  46. #define PCAL6524_INT_CLR 0x2a
  47. #define PCAL6524_IN_STATUS 0x2b
  48. #define PCAL6524_OUT_INDCONF 0x2c
  49. #define PCAL6524_DEBOUNCE 0x2d
  50. #define PCA_GPIO_MASK 0x00FF
  51. #define PCAL_GPIO_MASK 0x1f
  52. #define PCAL_PINCTRL_MASK 0xe0
  53. #define PCA_INT 0x0100
  54. #define PCA_PCAL 0x0200
  55. #define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
  56. #define PCA953X_TYPE 0x1000
  57. #define PCA957X_TYPE 0x2000
  58. #define PCA_TYPE_MASK 0xF000
  59. #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
  60. static const struct i2c_device_id pca953x_id[] = {
  61. { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
  62. { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
  63. { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
  64. { "pca9536", 4 | PCA953X_TYPE, },
  65. { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
  66. { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
  67. { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
  68. { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
  69. { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
  70. { "pca9556", 8 | PCA953X_TYPE, },
  71. { "pca9557", 8 | PCA953X_TYPE, },
  72. { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
  73. { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
  74. { "pca9698", 40 | PCA953X_TYPE, },
  75. { "pcal6524", 24 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
  76. { "pcal9555a", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
  77. { "max7310", 8 | PCA953X_TYPE, },
  78. { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
  79. { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
  80. { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
  81. { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
  82. { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
  83. { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
  84. { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
  85. { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
  86. { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
  87. { "tca9554", 8 | PCA953X_TYPE | PCA_INT, },
  88. { "xra1202", 8 | PCA953X_TYPE },
  89. { }
  90. };
  91. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  92. static const struct acpi_device_id pca953x_acpi_ids[] = {
  93. { "INT3491", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
  94. { }
  95. };
  96. MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
  97. #define MAX_BANK 5
  98. #define BANK_SZ 8
  99. #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
  100. struct pca953x_reg_config {
  101. int direction;
  102. int output;
  103. int input;
  104. };
  105. static const struct pca953x_reg_config pca953x_regs = {
  106. .direction = PCA953X_DIRECTION,
  107. .output = PCA953X_OUTPUT,
  108. .input = PCA953X_INPUT,
  109. };
  110. static const struct pca953x_reg_config pca957x_regs = {
  111. .direction = PCA957X_CFG,
  112. .output = PCA957X_OUT,
  113. .input = PCA957X_IN,
  114. };
  115. struct pca953x_chip {
  116. unsigned gpio_start;
  117. u8 reg_output[MAX_BANK];
  118. u8 reg_direction[MAX_BANK];
  119. struct mutex i2c_lock;
  120. #ifdef CONFIG_GPIO_PCA953X_IRQ
  121. struct mutex irq_lock;
  122. u8 irq_mask[MAX_BANK];
  123. u8 irq_stat[MAX_BANK];
  124. u8 irq_trig_raise[MAX_BANK];
  125. u8 irq_trig_fall[MAX_BANK];
  126. #endif
  127. struct i2c_client *client;
  128. struct gpio_chip gpio_chip;
  129. const char *const *names;
  130. unsigned long driver_data;
  131. struct regulator *regulator;
  132. const struct pca953x_reg_config *regs;
  133. int (*write_regs)(struct pca953x_chip *, int, u8 *);
  134. int (*read_regs)(struct pca953x_chip *, int, u8 *);
  135. };
  136. static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
  137. int off)
  138. {
  139. int ret;
  140. int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  141. int offset = off / BANK_SZ;
  142. ret = i2c_smbus_read_byte_data(chip->client,
  143. (reg << bank_shift) + offset);
  144. *val = ret;
  145. if (ret < 0) {
  146. dev_err(&chip->client->dev, "failed reading register\n");
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
  152. int off)
  153. {
  154. int ret;
  155. int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  156. int offset = off / BANK_SZ;
  157. ret = i2c_smbus_write_byte_data(chip->client,
  158. (reg << bank_shift) + offset, val);
  159. if (ret < 0) {
  160. dev_err(&chip->client->dev, "failed writing register\n");
  161. return ret;
  162. }
  163. return 0;
  164. }
  165. static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
  166. {
  167. return i2c_smbus_write_byte_data(chip->client, reg, *val);
  168. }
  169. static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
  170. {
  171. u16 word = get_unaligned((u16 *)val);
  172. return i2c_smbus_write_word_data(chip->client, reg << 1, word);
  173. }
  174. static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
  175. {
  176. int ret;
  177. ret = i2c_smbus_write_byte_data(chip->client, reg << 1, val[0]);
  178. if (ret < 0)
  179. return ret;
  180. return i2c_smbus_write_byte_data(chip->client, (reg << 1) + 1, val[1]);
  181. }
  182. static int pca953x_write_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
  183. {
  184. int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  185. int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
  186. int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
  187. return i2c_smbus_write_i2c_block_data(chip->client,
  188. pinctrl | addr | REG_ADDR_AI,
  189. NBANK(chip), val);
  190. }
  191. static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
  192. {
  193. int ret = 0;
  194. ret = chip->write_regs(chip, reg, val);
  195. if (ret < 0) {
  196. dev_err(&chip->client->dev, "failed writing register\n");
  197. return ret;
  198. }
  199. return 0;
  200. }
  201. static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
  202. {
  203. int ret;
  204. ret = i2c_smbus_read_byte_data(chip->client, reg);
  205. *val = ret;
  206. return ret;
  207. }
  208. static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
  209. {
  210. int ret;
  211. ret = i2c_smbus_read_word_data(chip->client, reg << 1);
  212. put_unaligned(ret, (u16 *)val);
  213. return ret;
  214. }
  215. static int pca953x_read_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
  216. {
  217. int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  218. int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
  219. int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
  220. return i2c_smbus_read_i2c_block_data(chip->client,
  221. pinctrl | addr | REG_ADDR_AI,
  222. NBANK(chip), val);
  223. }
  224. static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
  225. {
  226. int ret;
  227. ret = chip->read_regs(chip, reg, val);
  228. if (ret < 0) {
  229. dev_err(&chip->client->dev, "failed reading register\n");
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  235. {
  236. struct pca953x_chip *chip = gpiochip_get_data(gc);
  237. u8 reg_val;
  238. int ret;
  239. mutex_lock(&chip->i2c_lock);
  240. reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
  241. ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
  242. if (ret)
  243. goto exit;
  244. chip->reg_direction[off / BANK_SZ] = reg_val;
  245. exit:
  246. mutex_unlock(&chip->i2c_lock);
  247. return ret;
  248. }
  249. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  250. unsigned off, int val)
  251. {
  252. struct pca953x_chip *chip = gpiochip_get_data(gc);
  253. u8 reg_val;
  254. int ret;
  255. mutex_lock(&chip->i2c_lock);
  256. /* set output level */
  257. if (val)
  258. reg_val = chip->reg_output[off / BANK_SZ]
  259. | (1u << (off % BANK_SZ));
  260. else
  261. reg_val = chip->reg_output[off / BANK_SZ]
  262. & ~(1u << (off % BANK_SZ));
  263. ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
  264. if (ret)
  265. goto exit;
  266. chip->reg_output[off / BANK_SZ] = reg_val;
  267. /* then direction */
  268. reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
  269. ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
  270. if (ret)
  271. goto exit;
  272. chip->reg_direction[off / BANK_SZ] = reg_val;
  273. exit:
  274. mutex_unlock(&chip->i2c_lock);
  275. return ret;
  276. }
  277. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  278. {
  279. struct pca953x_chip *chip = gpiochip_get_data(gc);
  280. u32 reg_val;
  281. int ret;
  282. mutex_lock(&chip->i2c_lock);
  283. ret = pca953x_read_single(chip, chip->regs->input, &reg_val, off);
  284. mutex_unlock(&chip->i2c_lock);
  285. if (ret < 0) {
  286. /* NOTE: diagnostic already emitted; that's all we should
  287. * do unless gpio_*_value_cansleep() calls become different
  288. * from their nonsleeping siblings (and report faults).
  289. */
  290. return 0;
  291. }
  292. return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
  293. }
  294. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  295. {
  296. struct pca953x_chip *chip = gpiochip_get_data(gc);
  297. u8 reg_val;
  298. int ret;
  299. mutex_lock(&chip->i2c_lock);
  300. if (val)
  301. reg_val = chip->reg_output[off / BANK_SZ]
  302. | (1u << (off % BANK_SZ));
  303. else
  304. reg_val = chip->reg_output[off / BANK_SZ]
  305. & ~(1u << (off % BANK_SZ));
  306. ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
  307. if (ret)
  308. goto exit;
  309. chip->reg_output[off / BANK_SZ] = reg_val;
  310. exit:
  311. mutex_unlock(&chip->i2c_lock);
  312. }
  313. static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
  314. {
  315. struct pca953x_chip *chip = gpiochip_get_data(gc);
  316. u32 reg_val;
  317. int ret;
  318. mutex_lock(&chip->i2c_lock);
  319. ret = pca953x_read_single(chip, chip->regs->direction, &reg_val, off);
  320. mutex_unlock(&chip->i2c_lock);
  321. if (ret < 0)
  322. return ret;
  323. return !!(reg_val & (1u << (off % BANK_SZ)));
  324. }
  325. static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
  326. unsigned long *mask, unsigned long *bits)
  327. {
  328. struct pca953x_chip *chip = gpiochip_get_data(gc);
  329. unsigned int bank_mask, bank_val;
  330. int bank_shift, bank;
  331. u8 reg_val[MAX_BANK];
  332. int ret;
  333. bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  334. mutex_lock(&chip->i2c_lock);
  335. memcpy(reg_val, chip->reg_output, NBANK(chip));
  336. for (bank = 0; bank < NBANK(chip); bank++) {
  337. bank_mask = mask[bank / sizeof(*mask)] >>
  338. ((bank % sizeof(*mask)) * 8);
  339. if (bank_mask) {
  340. bank_val = bits[bank / sizeof(*bits)] >>
  341. ((bank % sizeof(*bits)) * 8);
  342. bank_val &= bank_mask;
  343. reg_val[bank] = (reg_val[bank] & ~bank_mask) | bank_val;
  344. }
  345. }
  346. ret = i2c_smbus_write_i2c_block_data(chip->client,
  347. chip->regs->output << bank_shift,
  348. NBANK(chip), reg_val);
  349. if (ret)
  350. goto exit;
  351. memcpy(chip->reg_output, reg_val, NBANK(chip));
  352. exit:
  353. mutex_unlock(&chip->i2c_lock);
  354. }
  355. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  356. {
  357. struct gpio_chip *gc;
  358. gc = &chip->gpio_chip;
  359. gc->direction_input = pca953x_gpio_direction_input;
  360. gc->direction_output = pca953x_gpio_direction_output;
  361. gc->get = pca953x_gpio_get_value;
  362. gc->set = pca953x_gpio_set_value;
  363. gc->get_direction = pca953x_gpio_get_direction;
  364. gc->set_multiple = pca953x_gpio_set_multiple;
  365. gc->can_sleep = true;
  366. gc->base = chip->gpio_start;
  367. gc->ngpio = gpios;
  368. gc->label = chip->client->name;
  369. gc->parent = &chip->client->dev;
  370. gc->owner = THIS_MODULE;
  371. gc->names = chip->names;
  372. }
  373. #ifdef CONFIG_GPIO_PCA953X_IRQ
  374. static void pca953x_irq_mask(struct irq_data *d)
  375. {
  376. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  377. struct pca953x_chip *chip = gpiochip_get_data(gc);
  378. chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
  379. }
  380. static void pca953x_irq_unmask(struct irq_data *d)
  381. {
  382. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  383. struct pca953x_chip *chip = gpiochip_get_data(gc);
  384. chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
  385. }
  386. static void pca953x_irq_bus_lock(struct irq_data *d)
  387. {
  388. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  389. struct pca953x_chip *chip = gpiochip_get_data(gc);
  390. mutex_lock(&chip->irq_lock);
  391. }
  392. static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
  393. {
  394. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  395. struct pca953x_chip *chip = gpiochip_get_data(gc);
  396. u8 new_irqs;
  397. int level, i;
  398. u8 invert_irq_mask[MAX_BANK];
  399. if (chip->driver_data & PCA_PCAL) {
  400. /* Enable latch on interrupt-enabled inputs */
  401. pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
  402. for (i = 0; i < NBANK(chip); i++)
  403. invert_irq_mask[i] = ~chip->irq_mask[i];
  404. /* Unmask enabled interrupts */
  405. pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
  406. }
  407. /* Look for any newly setup interrupt */
  408. for (i = 0; i < NBANK(chip); i++) {
  409. new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
  410. new_irqs &= ~chip->reg_direction[i];
  411. while (new_irqs) {
  412. level = __ffs(new_irqs);
  413. pca953x_gpio_direction_input(&chip->gpio_chip,
  414. level + (BANK_SZ * i));
  415. new_irqs &= ~(1 << level);
  416. }
  417. }
  418. mutex_unlock(&chip->irq_lock);
  419. }
  420. static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
  421. {
  422. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  423. struct pca953x_chip *chip = gpiochip_get_data(gc);
  424. int bank_nb = d->hwirq / BANK_SZ;
  425. u8 mask = 1 << (d->hwirq % BANK_SZ);
  426. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  427. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  428. d->irq, type);
  429. return -EINVAL;
  430. }
  431. if (type & IRQ_TYPE_EDGE_FALLING)
  432. chip->irq_trig_fall[bank_nb] |= mask;
  433. else
  434. chip->irq_trig_fall[bank_nb] &= ~mask;
  435. if (type & IRQ_TYPE_EDGE_RISING)
  436. chip->irq_trig_raise[bank_nb] |= mask;
  437. else
  438. chip->irq_trig_raise[bank_nb] &= ~mask;
  439. return 0;
  440. }
  441. static void pca953x_irq_shutdown(struct irq_data *d)
  442. {
  443. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  444. u8 mask = 1 << (d->hwirq % BANK_SZ);
  445. chip->irq_trig_raise[d->hwirq / BANK_SZ] &= ~mask;
  446. chip->irq_trig_fall[d->hwirq / BANK_SZ] &= ~mask;
  447. }
  448. static struct irq_chip pca953x_irq_chip = {
  449. .name = "pca953x",
  450. .irq_mask = pca953x_irq_mask,
  451. .irq_unmask = pca953x_irq_unmask,
  452. .irq_bus_lock = pca953x_irq_bus_lock,
  453. .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
  454. .irq_set_type = pca953x_irq_set_type,
  455. .irq_shutdown = pca953x_irq_shutdown,
  456. };
  457. static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
  458. {
  459. u8 cur_stat[MAX_BANK];
  460. u8 old_stat[MAX_BANK];
  461. bool pending_seen = false;
  462. bool trigger_seen = false;
  463. u8 trigger[MAX_BANK];
  464. int ret, i;
  465. if (chip->driver_data & PCA_PCAL) {
  466. /* Read the current interrupt status from the device */
  467. ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
  468. if (ret)
  469. return false;
  470. /* Check latched inputs and clear interrupt status */
  471. ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
  472. if (ret)
  473. return false;
  474. for (i = 0; i < NBANK(chip); i++) {
  475. /* Apply filter for rising/falling edge selection */
  476. pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
  477. (cur_stat[i] & chip->irq_trig_raise[i]);
  478. pending[i] &= trigger[i];
  479. if (pending[i])
  480. pending_seen = true;
  481. }
  482. return pending_seen;
  483. }
  484. ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
  485. if (ret)
  486. return false;
  487. /* Remove output pins from the equation */
  488. for (i = 0; i < NBANK(chip); i++)
  489. cur_stat[i] &= chip->reg_direction[i];
  490. memcpy(old_stat, chip->irq_stat, NBANK(chip));
  491. for (i = 0; i < NBANK(chip); i++) {
  492. trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
  493. if (trigger[i])
  494. trigger_seen = true;
  495. }
  496. if (!trigger_seen)
  497. return false;
  498. memcpy(chip->irq_stat, cur_stat, NBANK(chip));
  499. for (i = 0; i < NBANK(chip); i++) {
  500. pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
  501. (cur_stat[i] & chip->irq_trig_raise[i]);
  502. pending[i] &= trigger[i];
  503. if (pending[i])
  504. pending_seen = true;
  505. }
  506. return pending_seen;
  507. }
  508. static irqreturn_t pca953x_irq_handler(int irq, void *devid)
  509. {
  510. struct pca953x_chip *chip = devid;
  511. u8 pending[MAX_BANK];
  512. u8 level;
  513. unsigned nhandled = 0;
  514. int i;
  515. if (!pca953x_irq_pending(chip, pending))
  516. return IRQ_NONE;
  517. for (i = 0; i < NBANK(chip); i++) {
  518. while (pending[i]) {
  519. level = __ffs(pending[i]);
  520. handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
  521. level + (BANK_SZ * i)));
  522. pending[i] &= ~(1 << level);
  523. nhandled++;
  524. }
  525. }
  526. return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
  527. }
  528. static int pca953x_irq_setup(struct pca953x_chip *chip,
  529. int irq_base)
  530. {
  531. struct i2c_client *client = chip->client;
  532. int ret, i;
  533. if (client->irq && irq_base != -1
  534. && (chip->driver_data & PCA_INT)) {
  535. ret = pca953x_read_regs(chip,
  536. chip->regs->input, chip->irq_stat);
  537. if (ret)
  538. return ret;
  539. /*
  540. * There is no way to know which GPIO line generated the
  541. * interrupt. We have to rely on the previous read for
  542. * this purpose.
  543. */
  544. for (i = 0; i < NBANK(chip); i++)
  545. chip->irq_stat[i] &= chip->reg_direction[i];
  546. mutex_init(&chip->irq_lock);
  547. ret = devm_request_threaded_irq(&client->dev,
  548. client->irq,
  549. NULL,
  550. pca953x_irq_handler,
  551. IRQF_TRIGGER_LOW | IRQF_ONESHOT |
  552. IRQF_SHARED,
  553. dev_name(&client->dev), chip);
  554. if (ret) {
  555. dev_err(&client->dev, "failed to request irq %d\n",
  556. client->irq);
  557. return ret;
  558. }
  559. ret = gpiochip_irqchip_add_nested(&chip->gpio_chip,
  560. &pca953x_irq_chip,
  561. irq_base,
  562. handle_simple_irq,
  563. IRQ_TYPE_NONE);
  564. if (ret) {
  565. dev_err(&client->dev,
  566. "could not connect irqchip to gpiochip\n");
  567. return ret;
  568. }
  569. gpiochip_set_nested_irqchip(&chip->gpio_chip,
  570. &pca953x_irq_chip,
  571. client->irq);
  572. }
  573. return 0;
  574. }
  575. #else /* CONFIG_GPIO_PCA953X_IRQ */
  576. static int pca953x_irq_setup(struct pca953x_chip *chip,
  577. int irq_base)
  578. {
  579. struct i2c_client *client = chip->client;
  580. if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
  581. dev_warn(&client->dev, "interrupt support not compiled in\n");
  582. return 0;
  583. }
  584. #endif
  585. static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
  586. {
  587. int ret;
  588. u8 val[MAX_BANK];
  589. chip->regs = &pca953x_regs;
  590. ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
  591. if (ret)
  592. goto out;
  593. ret = pca953x_read_regs(chip, chip->regs->direction,
  594. chip->reg_direction);
  595. if (ret)
  596. goto out;
  597. /* set platform specific polarity inversion */
  598. if (invert)
  599. memset(val, 0xFF, NBANK(chip));
  600. else
  601. memset(val, 0, NBANK(chip));
  602. ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
  603. out:
  604. return ret;
  605. }
  606. static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
  607. {
  608. int ret;
  609. u8 val[MAX_BANK];
  610. chip->regs = &pca957x_regs;
  611. ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
  612. if (ret)
  613. goto out;
  614. ret = pca953x_read_regs(chip, chip->regs->direction,
  615. chip->reg_direction);
  616. if (ret)
  617. goto out;
  618. /* set platform specific polarity inversion */
  619. if (invert)
  620. memset(val, 0xFF, NBANK(chip));
  621. else
  622. memset(val, 0, NBANK(chip));
  623. ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
  624. if (ret)
  625. goto out;
  626. /* To enable register 6, 7 to control pull up and pull down */
  627. memset(val, 0x02, NBANK(chip));
  628. ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
  629. if (ret)
  630. goto out;
  631. return 0;
  632. out:
  633. return ret;
  634. }
  635. static const struct of_device_id pca953x_dt_ids[];
  636. static int pca953x_probe(struct i2c_client *client,
  637. const struct i2c_device_id *i2c_id)
  638. {
  639. struct pca953x_platform_data *pdata;
  640. struct pca953x_chip *chip;
  641. int irq_base = 0;
  642. int ret;
  643. u32 invert = 0;
  644. struct regulator *reg;
  645. chip = devm_kzalloc(&client->dev,
  646. sizeof(struct pca953x_chip), GFP_KERNEL);
  647. if (chip == NULL)
  648. return -ENOMEM;
  649. pdata = dev_get_platdata(&client->dev);
  650. if (pdata) {
  651. irq_base = pdata->irq_base;
  652. chip->gpio_start = pdata->gpio_base;
  653. invert = pdata->invert;
  654. chip->names = pdata->names;
  655. } else {
  656. struct gpio_desc *reset_gpio;
  657. chip->gpio_start = -1;
  658. irq_base = 0;
  659. /*
  660. * See if we need to de-assert a reset pin.
  661. *
  662. * There is no known ACPI-enabled platforms that are
  663. * using "reset" GPIO. Otherwise any of those platform
  664. * must use _DSD method with corresponding property.
  665. */
  666. reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  667. GPIOD_OUT_LOW);
  668. if (IS_ERR(reset_gpio))
  669. return PTR_ERR(reset_gpio);
  670. }
  671. chip->client = client;
  672. reg = devm_regulator_get(&client->dev, "vcc");
  673. if (IS_ERR(reg)) {
  674. ret = PTR_ERR(reg);
  675. if (ret != -EPROBE_DEFER)
  676. dev_err(&client->dev, "reg get err: %d\n", ret);
  677. return ret;
  678. }
  679. ret = regulator_enable(reg);
  680. if (ret) {
  681. dev_err(&client->dev, "reg en err: %d\n", ret);
  682. return ret;
  683. }
  684. chip->regulator = reg;
  685. if (i2c_id) {
  686. chip->driver_data = i2c_id->driver_data;
  687. } else {
  688. const struct acpi_device_id *acpi_id;
  689. struct device *dev = &client->dev;
  690. chip->driver_data = (uintptr_t)of_device_get_match_data(dev);
  691. if (!chip->driver_data) {
  692. acpi_id = acpi_match_device(pca953x_acpi_ids, dev);
  693. if (!acpi_id) {
  694. ret = -ENODEV;
  695. goto err_exit;
  696. }
  697. chip->driver_data = acpi_id->driver_data;
  698. }
  699. }
  700. mutex_init(&chip->i2c_lock);
  701. /*
  702. * In case we have an i2c-mux controlled by a GPIO provided by an
  703. * expander using the same driver higher on the device tree, read the
  704. * i2c adapter nesting depth and use the retrieved value as lockdep
  705. * subclass for chip->i2c_lock.
  706. *
  707. * REVISIT: This solution is not complete. It protects us from lockdep
  708. * false positives when the expander controlling the i2c-mux is on
  709. * a different level on the device tree, but not when it's on the same
  710. * level on a different branch (in which case the subclass number
  711. * would be the same).
  712. *
  713. * TODO: Once a correct solution is developed, a similar fix should be
  714. * applied to all other i2c-controlled GPIO expanders (and potentially
  715. * regmap-i2c).
  716. */
  717. lockdep_set_subclass(&chip->i2c_lock,
  718. i2c_adapter_depth(client->adapter));
  719. /* initialize cached registers from their original values.
  720. * we can't share this chip with another i2c master.
  721. */
  722. pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
  723. if (chip->gpio_chip.ngpio <= 8) {
  724. chip->write_regs = pca953x_write_regs_8;
  725. chip->read_regs = pca953x_read_regs_8;
  726. } else if (chip->gpio_chip.ngpio >= 24) {
  727. chip->write_regs = pca953x_write_regs_24;
  728. chip->read_regs = pca953x_read_regs_24;
  729. } else {
  730. if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
  731. chip->write_regs = pca953x_write_regs_16;
  732. else
  733. chip->write_regs = pca957x_write_regs_16;
  734. chip->read_regs = pca953x_read_regs_16;
  735. }
  736. if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
  737. ret = device_pca953x_init(chip, invert);
  738. else
  739. ret = device_pca957x_init(chip, invert);
  740. if (ret)
  741. goto err_exit;
  742. ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
  743. if (ret)
  744. goto err_exit;
  745. ret = pca953x_irq_setup(chip, irq_base);
  746. if (ret)
  747. goto err_exit;
  748. if (pdata && pdata->setup) {
  749. ret = pdata->setup(client, chip->gpio_chip.base,
  750. chip->gpio_chip.ngpio, pdata->context);
  751. if (ret < 0)
  752. dev_warn(&client->dev, "setup failed, %d\n", ret);
  753. }
  754. i2c_set_clientdata(client, chip);
  755. return 0;
  756. err_exit:
  757. regulator_disable(chip->regulator);
  758. return ret;
  759. }
  760. static int pca953x_remove(struct i2c_client *client)
  761. {
  762. struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
  763. struct pca953x_chip *chip = i2c_get_clientdata(client);
  764. int ret;
  765. if (pdata && pdata->teardown) {
  766. ret = pdata->teardown(client, chip->gpio_chip.base,
  767. chip->gpio_chip.ngpio, pdata->context);
  768. if (ret < 0)
  769. dev_err(&client->dev, "%s failed, %d\n",
  770. "teardown", ret);
  771. } else {
  772. ret = 0;
  773. }
  774. regulator_disable(chip->regulator);
  775. return ret;
  776. }
  777. /* convenience to stop overlong match-table lines */
  778. #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
  779. #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
  780. static const struct of_device_id pca953x_dt_ids[] = {
  781. { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
  782. { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
  783. { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
  784. { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
  785. { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
  786. { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
  787. { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
  788. { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
  789. { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
  790. { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
  791. { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
  792. { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
  793. { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
  794. { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
  795. { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
  796. { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
  797. { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
  798. { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
  799. { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
  800. { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
  801. { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
  802. { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
  803. { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
  804. { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
  805. { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
  806. { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
  807. { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
  808. { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
  809. { }
  810. };
  811. MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
  812. static struct i2c_driver pca953x_driver = {
  813. .driver = {
  814. .name = "pca953x",
  815. .of_match_table = pca953x_dt_ids,
  816. .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
  817. },
  818. .probe = pca953x_probe,
  819. .remove = pca953x_remove,
  820. .id_table = pca953x_id,
  821. };
  822. static int __init pca953x_init(void)
  823. {
  824. return i2c_add_driver(&pca953x_driver);
  825. }
  826. /* register after i2c postcore initcall and before
  827. * subsys initcalls that may rely on these GPIOs
  828. */
  829. subsys_initcall(pca953x_init);
  830. static void __exit pca953x_exit(void)
  831. {
  832. i2c_del_driver(&pca953x_driver);
  833. }
  834. module_exit(pca953x_exit);
  835. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  836. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  837. MODULE_LICENSE("GPL");