gpio-pca953x.c 24 KB

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