gpio-pca953x.c 25 KB

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