gpio-pca953x.c 26 KB

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