gpio-pca953x.c 24 KB

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