gpio-pca953x.c 25 KB

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