gpio-pca953x.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCA953x 4/8/16/24/40 bit I/O ports
  4. *
  5. * Copyright (C) 2005 Ben Gardner <[email protected]>
  6. * Copyright (C) 2007 Marvell International Ltd.
  7. *
  8. * Derived from drivers/i2c/chips/pca9539.c
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_data/pca953x.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <asm/unaligned.h>
  24. #define PCA953X_INPUT 0x00
  25. #define PCA953X_OUTPUT 0x01
  26. #define PCA953X_INVERT 0x02
  27. #define PCA953X_DIRECTION 0x03
  28. #define REG_ADDR_MASK GENMASK(5, 0)
  29. #define REG_ADDR_EXT BIT(6)
  30. #define REG_ADDR_AI BIT(7)
  31. #define PCA957X_IN 0x00
  32. #define PCA957X_INVRT 0x01
  33. #define PCA957X_BKEN 0x02
  34. #define PCA957X_PUPD 0x03
  35. #define PCA957X_CFG 0x04
  36. #define PCA957X_OUT 0x05
  37. #define PCA957X_MSK 0x06
  38. #define PCA957X_INTS 0x07
  39. #define PCAL953X_OUT_STRENGTH 0x20
  40. #define PCAL953X_IN_LATCH 0x22
  41. #define PCAL953X_PULL_EN 0x23
  42. #define PCAL953X_PULL_SEL 0x24
  43. #define PCAL953X_INT_MASK 0x25
  44. #define PCAL953X_INT_STAT 0x26
  45. #define PCAL953X_OUT_CONF 0x27
  46. #define PCAL6524_INT_EDGE 0x28
  47. #define PCAL6524_INT_CLR 0x2a
  48. #define PCAL6524_IN_STATUS 0x2b
  49. #define PCAL6524_OUT_INDCONF 0x2c
  50. #define PCAL6524_DEBOUNCE 0x2d
  51. #define PCA_GPIO_MASK GENMASK(7, 0)
  52. #define PCAL_GPIO_MASK GENMASK(4, 0)
  53. #define PCAL_PINCTRL_MASK GENMASK(6, 5)
  54. #define PCA_INT BIT(8)
  55. #define PCA_PCAL BIT(9)
  56. #define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
  57. #define PCA953X_TYPE BIT(12)
  58. #define PCA957X_TYPE BIT(13)
  59. #define PCAL653X_TYPE BIT(14)
  60. #define PCA_TYPE_MASK GENMASK(15, 12)
  61. #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
  62. static const struct i2c_device_id pca953x_id[] = {
  63. { "pca6408", 8 | PCA953X_TYPE | PCA_INT, },
  64. { "pca6416", 16 | PCA953X_TYPE | PCA_INT, },
  65. { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
  66. { "pca9506", 40 | PCA953X_TYPE | PCA_INT, },
  67. { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
  68. { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
  69. { "pca9536", 4 | PCA953X_TYPE, },
  70. { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
  71. { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
  72. { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
  73. { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
  74. { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
  75. { "pca9556", 8 | PCA953X_TYPE, },
  76. { "pca9557", 8 | PCA953X_TYPE, },
  77. { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
  78. { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
  79. { "pca9698", 40 | PCA953X_TYPE, },
  80. { "pcal6408", 8 | PCA953X_TYPE | PCA_LATCH_INT, },
  81. { "pcal6416", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
  82. { "pcal6524", 24 | PCA953X_TYPE | PCA_LATCH_INT, },
  83. { "pcal6534", 34 | PCAL653X_TYPE | PCA_LATCH_INT, },
  84. { "pcal9535", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
  85. { "pcal9554b", 8 | PCA953X_TYPE | PCA_LATCH_INT, },
  86. { "pcal9555a", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
  87. { "max7310", 8 | PCA953X_TYPE, },
  88. { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
  89. { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
  90. { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
  91. { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
  92. { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
  93. { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
  94. { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
  95. { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
  96. { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
  97. { "tca9554", 8 | PCA953X_TYPE | PCA_INT, },
  98. { "xra1202", 8 | PCA953X_TYPE },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  102. #ifdef CONFIG_GPIO_PCA953X_IRQ
  103. #include <linux/dmi.h>
  104. static const struct acpi_gpio_params pca953x_irq_gpios = { 0, 0, true };
  105. static const struct acpi_gpio_mapping pca953x_acpi_irq_gpios[] = {
  106. { "irq-gpios", &pca953x_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
  107. { }
  108. };
  109. static int pca953x_acpi_get_irq(struct device *dev)
  110. {
  111. int ret;
  112. ret = devm_acpi_dev_add_driver_gpios(dev, pca953x_acpi_irq_gpios);
  113. if (ret)
  114. dev_warn(dev, "can't add GPIO ACPI mapping\n");
  115. ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq-gpios", 0);
  116. if (ret < 0)
  117. return ret;
  118. dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
  119. return ret;
  120. }
  121. static const struct dmi_system_id pca953x_dmi_acpi_irq_info[] = {
  122. {
  123. /*
  124. * On Intel Galileo Gen 2 board the IRQ pin of one of
  125. * the I²C GPIO expanders, which has GpioInt() resource,
  126. * is provided as an absolute number instead of being
  127. * relative. Since first controller (gpio-sch.c) and
  128. * second (gpio-dwapb.c) are at the fixed bases, we may
  129. * safely refer to the number in the global space to get
  130. * an IRQ out of it.
  131. */
  132. .matches = {
  133. DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
  134. },
  135. },
  136. {}
  137. };
  138. #endif
  139. static const struct acpi_device_id pca953x_acpi_ids[] = {
  140. { "INT3491", 16 | PCA953X_TYPE | PCA_LATCH_INT, },
  141. { }
  142. };
  143. MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
  144. #define MAX_BANK 5
  145. #define BANK_SZ 8
  146. #define MAX_LINE (MAX_BANK * BANK_SZ)
  147. #define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
  148. struct pca953x_reg_config {
  149. int direction;
  150. int output;
  151. int input;
  152. int invert;
  153. };
  154. static const struct pca953x_reg_config pca953x_regs = {
  155. .direction = PCA953X_DIRECTION,
  156. .output = PCA953X_OUTPUT,
  157. .input = PCA953X_INPUT,
  158. .invert = PCA953X_INVERT,
  159. };
  160. static const struct pca953x_reg_config pca957x_regs = {
  161. .direction = PCA957X_CFG,
  162. .output = PCA957X_OUT,
  163. .input = PCA957X_IN,
  164. .invert = PCA957X_INVRT,
  165. };
  166. struct pca953x_chip {
  167. unsigned gpio_start;
  168. struct mutex i2c_lock;
  169. struct regmap *regmap;
  170. #ifdef CONFIG_GPIO_PCA953X_IRQ
  171. struct mutex irq_lock;
  172. DECLARE_BITMAP(irq_mask, MAX_LINE);
  173. DECLARE_BITMAP(irq_stat, MAX_LINE);
  174. DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
  175. DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
  176. #endif
  177. atomic_t wakeup_path;
  178. struct i2c_client *client;
  179. struct gpio_chip gpio_chip;
  180. const char *const *names;
  181. unsigned long driver_data;
  182. struct regulator *regulator;
  183. const struct pca953x_reg_config *regs;
  184. u8 (*recalc_addr)(struct pca953x_chip *chip, int reg, int off);
  185. bool (*check_reg)(struct pca953x_chip *chip, unsigned int reg,
  186. u32 checkbank);
  187. };
  188. static int pca953x_bank_shift(struct pca953x_chip *chip)
  189. {
  190. return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
  191. }
  192. #define PCA953x_BANK_INPUT BIT(0)
  193. #define PCA953x_BANK_OUTPUT BIT(1)
  194. #define PCA953x_BANK_POLARITY BIT(2)
  195. #define PCA953x_BANK_CONFIG BIT(3)
  196. #define PCA957x_BANK_INPUT BIT(0)
  197. #define PCA957x_BANK_POLARITY BIT(1)
  198. #define PCA957x_BANK_BUSHOLD BIT(2)
  199. #define PCA957x_BANK_CONFIG BIT(4)
  200. #define PCA957x_BANK_OUTPUT BIT(5)
  201. #define PCAL9xxx_BANK_IN_LATCH BIT(8 + 2)
  202. #define PCAL9xxx_BANK_PULL_EN BIT(8 + 3)
  203. #define PCAL9xxx_BANK_PULL_SEL BIT(8 + 4)
  204. #define PCAL9xxx_BANK_IRQ_MASK BIT(8 + 5)
  205. #define PCAL9xxx_BANK_IRQ_STAT BIT(8 + 6)
  206. /*
  207. * We care about the following registers:
  208. * - Standard set, below 0x40, each port can be replicated up to 8 times
  209. * - PCA953x standard
  210. * Input port 0x00 + 0 * bank_size R
  211. * Output port 0x00 + 1 * bank_size RW
  212. * Polarity Inversion port 0x00 + 2 * bank_size RW
  213. * Configuration port 0x00 + 3 * bank_size RW
  214. * - PCA957x with mixed up registers
  215. * Input port 0x00 + 0 * bank_size R
  216. * Polarity Inversion port 0x00 + 1 * bank_size RW
  217. * Bus hold port 0x00 + 2 * bank_size RW
  218. * Configuration port 0x00 + 4 * bank_size RW
  219. * Output port 0x00 + 5 * bank_size RW
  220. *
  221. * - Extended set, above 0x40, often chip specific.
  222. * - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
  223. * Input latch register 0x40 + 2 * bank_size RW
  224. * Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW
  225. * Pull-up/pull-down select reg 0x40 + 4 * bank_size RW
  226. * Interrupt mask register 0x40 + 5 * bank_size RW
  227. * Interrupt status register 0x40 + 6 * bank_size R
  228. *
  229. * - Registers with bit 0x80 set, the AI bit
  230. * The bit is cleared and the registers fall into one of the
  231. * categories above.
  232. */
  233. static bool pca953x_check_register(struct pca953x_chip *chip, unsigned int reg,
  234. u32 checkbank)
  235. {
  236. int bank_shift = pca953x_bank_shift(chip);
  237. int bank = (reg & REG_ADDR_MASK) >> bank_shift;
  238. int offset = reg & (BIT(bank_shift) - 1);
  239. /* Special PCAL extended register check. */
  240. if (reg & REG_ADDR_EXT) {
  241. if (!(chip->driver_data & PCA_PCAL))
  242. return false;
  243. bank += 8;
  244. }
  245. /* Register is not in the matching bank. */
  246. if (!(BIT(bank) & checkbank))
  247. return false;
  248. /* Register is not within allowed range of bank. */
  249. if (offset >= NBANK(chip))
  250. return false;
  251. return true;
  252. }
  253. /*
  254. * Unfortunately, whilst the PCAL6534 chip (and compatibles) broadly follow the
  255. * same register layout as the PCAL6524, the spacing of the registers has been
  256. * fundamentally altered by compacting them and thus does not obey the same
  257. * rules, including being able to use bit shifting to determine bank. These
  258. * chips hence need special handling here.
  259. */
  260. static bool pcal6534_check_register(struct pca953x_chip *chip, unsigned int reg,
  261. u32 checkbank)
  262. {
  263. int bank;
  264. int offset;
  265. if (reg >= 0x30) {
  266. /*
  267. * Reserved block between 14h and 2Fh does not align on
  268. * expected bank boundaries like other devices.
  269. */
  270. int temp = reg - 0x30;
  271. bank = temp / NBANK(chip);
  272. offset = temp - (bank * NBANK(chip));
  273. bank += 8;
  274. } else if (reg >= 0x54) {
  275. /*
  276. * Handle lack of reserved registers after output port
  277. * configuration register to form a bank.
  278. */
  279. int temp = reg - 0x54;
  280. bank = temp / NBANK(chip);
  281. offset = temp - (bank * NBANK(chip));
  282. bank += 16;
  283. } else {
  284. bank = reg / NBANK(chip);
  285. offset = reg - (bank * NBANK(chip));
  286. }
  287. /* Register is not in the matching bank. */
  288. if (!(BIT(bank) & checkbank))
  289. return false;
  290. /* Register is not within allowed range of bank. */
  291. if (offset >= NBANK(chip))
  292. return false;
  293. return true;
  294. }
  295. static bool pca953x_readable_register(struct device *dev, unsigned int reg)
  296. {
  297. struct pca953x_chip *chip = dev_get_drvdata(dev);
  298. u32 bank;
  299. if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
  300. bank = PCA957x_BANK_INPUT | PCA957x_BANK_OUTPUT |
  301. PCA957x_BANK_POLARITY | PCA957x_BANK_CONFIG |
  302. PCA957x_BANK_BUSHOLD;
  303. } else {
  304. bank = PCA953x_BANK_INPUT | PCA953x_BANK_OUTPUT |
  305. PCA953x_BANK_POLARITY | PCA953x_BANK_CONFIG;
  306. }
  307. if (chip->driver_data & PCA_PCAL) {
  308. bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
  309. PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
  310. PCAL9xxx_BANK_IRQ_STAT;
  311. }
  312. return chip->check_reg(chip, reg, bank);
  313. }
  314. static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
  315. {
  316. struct pca953x_chip *chip = dev_get_drvdata(dev);
  317. u32 bank;
  318. if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
  319. bank = PCA957x_BANK_OUTPUT | PCA957x_BANK_POLARITY |
  320. PCA957x_BANK_CONFIG | PCA957x_BANK_BUSHOLD;
  321. } else {
  322. bank = PCA953x_BANK_OUTPUT | PCA953x_BANK_POLARITY |
  323. PCA953x_BANK_CONFIG;
  324. }
  325. if (chip->driver_data & PCA_PCAL)
  326. bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
  327. PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
  328. return chip->check_reg(chip, reg, bank);
  329. }
  330. static bool pca953x_volatile_register(struct device *dev, unsigned int reg)
  331. {
  332. struct pca953x_chip *chip = dev_get_drvdata(dev);
  333. u32 bank;
  334. if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE)
  335. bank = PCA957x_BANK_INPUT;
  336. else
  337. bank = PCA953x_BANK_INPUT;
  338. if (chip->driver_data & PCA_PCAL)
  339. bank |= PCAL9xxx_BANK_IRQ_STAT;
  340. return chip->check_reg(chip, reg, bank);
  341. }
  342. static const struct regmap_config pca953x_i2c_regmap = {
  343. .reg_bits = 8,
  344. .val_bits = 8,
  345. .use_single_read = true,
  346. .use_single_write = true,
  347. .readable_reg = pca953x_readable_register,
  348. .writeable_reg = pca953x_writeable_register,
  349. .volatile_reg = pca953x_volatile_register,
  350. .disable_locking = true,
  351. .cache_type = REGCACHE_RBTREE,
  352. .max_register = 0x7f,
  353. };
  354. static const struct regmap_config pca953x_ai_i2c_regmap = {
  355. .reg_bits = 8,
  356. .val_bits = 8,
  357. .read_flag_mask = REG_ADDR_AI,
  358. .write_flag_mask = REG_ADDR_AI,
  359. .readable_reg = pca953x_readable_register,
  360. .writeable_reg = pca953x_writeable_register,
  361. .volatile_reg = pca953x_volatile_register,
  362. .disable_locking = true,
  363. .cache_type = REGCACHE_RBTREE,
  364. .max_register = 0x7f,
  365. };
  366. static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off)
  367. {
  368. int bank_shift = pca953x_bank_shift(chip);
  369. int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
  370. int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
  371. u8 regaddr = pinctrl | addr | (off / BANK_SZ);
  372. return regaddr;
  373. }
  374. /*
  375. * The PCAL6534 and compatible chips have altered bank alignment that doesn't
  376. * fit within the bit shifting scheme used for other devices.
  377. */
  378. static u8 pcal6534_recalc_addr(struct pca953x_chip *chip, int reg, int off)
  379. {
  380. int addr;
  381. int pinctrl;
  382. addr = (reg & PCAL_GPIO_MASK) * NBANK(chip);
  383. switch (reg) {
  384. case PCAL953X_OUT_STRENGTH:
  385. case PCAL953X_IN_LATCH:
  386. case PCAL953X_PULL_EN:
  387. case PCAL953X_PULL_SEL:
  388. case PCAL953X_INT_MASK:
  389. case PCAL953X_INT_STAT:
  390. case PCAL953X_OUT_CONF:
  391. pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x20;
  392. break;
  393. case PCAL6524_INT_EDGE:
  394. case PCAL6524_INT_CLR:
  395. case PCAL6524_IN_STATUS:
  396. case PCAL6524_OUT_INDCONF:
  397. case PCAL6524_DEBOUNCE:
  398. pinctrl = ((reg & PCAL_PINCTRL_MASK) >> 1) + 0x1c;
  399. break;
  400. default:
  401. pinctrl = 0;
  402. break;
  403. }
  404. return pinctrl + addr + (off / BANK_SZ);
  405. }
  406. static int pca953x_write_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
  407. {
  408. u8 regaddr = chip->recalc_addr(chip, reg, 0);
  409. u8 value[MAX_BANK];
  410. int i, ret;
  411. for (i = 0; i < NBANK(chip); i++)
  412. value[i] = bitmap_get_value8(val, i * BANK_SZ);
  413. ret = regmap_bulk_write(chip->regmap, regaddr, value, NBANK(chip));
  414. if (ret < 0) {
  415. dev_err(&chip->client->dev, "failed writing register\n");
  416. return ret;
  417. }
  418. return 0;
  419. }
  420. static int pca953x_read_regs(struct pca953x_chip *chip, int reg, unsigned long *val)
  421. {
  422. u8 regaddr = chip->recalc_addr(chip, reg, 0);
  423. u8 value[MAX_BANK];
  424. int i, ret;
  425. ret = regmap_bulk_read(chip->regmap, regaddr, value, NBANK(chip));
  426. if (ret < 0) {
  427. dev_err(&chip->client->dev, "failed reading register\n");
  428. return ret;
  429. }
  430. for (i = 0; i < NBANK(chip); i++)
  431. bitmap_set_value8(val, value[i], i * BANK_SZ);
  432. return 0;
  433. }
  434. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  435. {
  436. struct pca953x_chip *chip = gpiochip_get_data(gc);
  437. u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off);
  438. u8 bit = BIT(off % BANK_SZ);
  439. int ret;
  440. mutex_lock(&chip->i2c_lock);
  441. ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
  442. mutex_unlock(&chip->i2c_lock);
  443. return ret;
  444. }
  445. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  446. unsigned off, int val)
  447. {
  448. struct pca953x_chip *chip = gpiochip_get_data(gc);
  449. u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off);
  450. u8 outreg = chip->recalc_addr(chip, chip->regs->output, off);
  451. u8 bit = BIT(off % BANK_SZ);
  452. int ret;
  453. mutex_lock(&chip->i2c_lock);
  454. /* set output level */
  455. ret = regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
  456. if (ret)
  457. goto exit;
  458. /* then direction */
  459. ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
  460. exit:
  461. mutex_unlock(&chip->i2c_lock);
  462. return ret;
  463. }
  464. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  465. {
  466. struct pca953x_chip *chip = gpiochip_get_data(gc);
  467. u8 inreg = chip->recalc_addr(chip, chip->regs->input, off);
  468. u8 bit = BIT(off % BANK_SZ);
  469. u32 reg_val;
  470. int ret;
  471. mutex_lock(&chip->i2c_lock);
  472. ret = regmap_read(chip->regmap, inreg, &reg_val);
  473. mutex_unlock(&chip->i2c_lock);
  474. if (ret < 0)
  475. return ret;
  476. return !!(reg_val & bit);
  477. }
  478. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  479. {
  480. struct pca953x_chip *chip = gpiochip_get_data(gc);
  481. u8 outreg = chip->recalc_addr(chip, chip->regs->output, off);
  482. u8 bit = BIT(off % BANK_SZ);
  483. mutex_lock(&chip->i2c_lock);
  484. regmap_write_bits(chip->regmap, outreg, bit, val ? bit : 0);
  485. mutex_unlock(&chip->i2c_lock);
  486. }
  487. static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
  488. {
  489. struct pca953x_chip *chip = gpiochip_get_data(gc);
  490. u8 dirreg = chip->recalc_addr(chip, chip->regs->direction, off);
  491. u8 bit = BIT(off % BANK_SZ);
  492. u32 reg_val;
  493. int ret;
  494. mutex_lock(&chip->i2c_lock);
  495. ret = regmap_read(chip->regmap, dirreg, &reg_val);
  496. mutex_unlock(&chip->i2c_lock);
  497. if (ret < 0)
  498. return ret;
  499. if (reg_val & bit)
  500. return GPIO_LINE_DIRECTION_IN;
  501. return GPIO_LINE_DIRECTION_OUT;
  502. }
  503. static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
  504. unsigned long *mask, unsigned long *bits)
  505. {
  506. struct pca953x_chip *chip = gpiochip_get_data(gc);
  507. DECLARE_BITMAP(reg_val, MAX_LINE);
  508. int ret;
  509. mutex_lock(&chip->i2c_lock);
  510. ret = pca953x_read_regs(chip, chip->regs->input, reg_val);
  511. mutex_unlock(&chip->i2c_lock);
  512. if (ret)
  513. return ret;
  514. bitmap_replace(bits, bits, reg_val, mask, gc->ngpio);
  515. return 0;
  516. }
  517. static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
  518. unsigned long *mask, unsigned long *bits)
  519. {
  520. struct pca953x_chip *chip = gpiochip_get_data(gc);
  521. DECLARE_BITMAP(reg_val, MAX_LINE);
  522. int ret;
  523. mutex_lock(&chip->i2c_lock);
  524. ret = pca953x_read_regs(chip, chip->regs->output, reg_val);
  525. if (ret)
  526. goto exit;
  527. bitmap_replace(reg_val, reg_val, bits, mask, gc->ngpio);
  528. pca953x_write_regs(chip, chip->regs->output, reg_val);
  529. exit:
  530. mutex_unlock(&chip->i2c_lock);
  531. }
  532. static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
  533. unsigned int offset,
  534. unsigned long config)
  535. {
  536. enum pin_config_param param = pinconf_to_config_param(config);
  537. u8 pull_en_reg = chip->recalc_addr(chip, PCAL953X_PULL_EN, offset);
  538. u8 pull_sel_reg = chip->recalc_addr(chip, PCAL953X_PULL_SEL, offset);
  539. u8 bit = BIT(offset % BANK_SZ);
  540. int ret;
  541. /*
  542. * pull-up/pull-down configuration requires PCAL extended
  543. * registers
  544. */
  545. if (!(chip->driver_data & PCA_PCAL))
  546. return -ENOTSUPP;
  547. mutex_lock(&chip->i2c_lock);
  548. /* Configure pull-up/pull-down */
  549. if (param == PIN_CONFIG_BIAS_PULL_UP)
  550. ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
  551. else if (param == PIN_CONFIG_BIAS_PULL_DOWN)
  552. ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
  553. else
  554. ret = 0;
  555. if (ret)
  556. goto exit;
  557. /* Disable/Enable pull-up/pull-down */
  558. if (param == PIN_CONFIG_BIAS_DISABLE)
  559. ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
  560. else
  561. ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
  562. exit:
  563. mutex_unlock(&chip->i2c_lock);
  564. return ret;
  565. }
  566. static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  567. unsigned long config)
  568. {
  569. struct pca953x_chip *chip = gpiochip_get_data(gc);
  570. switch (pinconf_to_config_param(config)) {
  571. case PIN_CONFIG_BIAS_PULL_UP:
  572. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  573. case PIN_CONFIG_BIAS_PULL_DOWN:
  574. case PIN_CONFIG_BIAS_DISABLE:
  575. return pca953x_gpio_set_pull_up_down(chip, offset, config);
  576. default:
  577. return -ENOTSUPP;
  578. }
  579. }
  580. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  581. {
  582. struct gpio_chip *gc;
  583. gc = &chip->gpio_chip;
  584. gc->direction_input = pca953x_gpio_direction_input;
  585. gc->direction_output = pca953x_gpio_direction_output;
  586. gc->get = pca953x_gpio_get_value;
  587. gc->set = pca953x_gpio_set_value;
  588. gc->get_direction = pca953x_gpio_get_direction;
  589. gc->get_multiple = pca953x_gpio_get_multiple;
  590. gc->set_multiple = pca953x_gpio_set_multiple;
  591. gc->set_config = pca953x_gpio_set_config;
  592. gc->can_sleep = true;
  593. gc->base = chip->gpio_start;
  594. gc->ngpio = gpios;
  595. gc->label = dev_name(&chip->client->dev);
  596. gc->parent = &chip->client->dev;
  597. gc->owner = THIS_MODULE;
  598. gc->names = chip->names;
  599. }
  600. #ifdef CONFIG_GPIO_PCA953X_IRQ
  601. static void pca953x_irq_mask(struct irq_data *d)
  602. {
  603. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  604. struct pca953x_chip *chip = gpiochip_get_data(gc);
  605. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  606. clear_bit(hwirq, chip->irq_mask);
  607. gpiochip_disable_irq(gc, hwirq);
  608. }
  609. static void pca953x_irq_unmask(struct irq_data *d)
  610. {
  611. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  612. struct pca953x_chip *chip = gpiochip_get_data(gc);
  613. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  614. gpiochip_enable_irq(gc, hwirq);
  615. set_bit(hwirq, chip->irq_mask);
  616. }
  617. static int pca953x_irq_set_wake(struct irq_data *d, unsigned int on)
  618. {
  619. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  620. struct pca953x_chip *chip = gpiochip_get_data(gc);
  621. if (on)
  622. atomic_inc(&chip->wakeup_path);
  623. else
  624. atomic_dec(&chip->wakeup_path);
  625. return irq_set_irq_wake(chip->client->irq, on);
  626. }
  627. static void pca953x_irq_bus_lock(struct irq_data *d)
  628. {
  629. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  630. struct pca953x_chip *chip = gpiochip_get_data(gc);
  631. mutex_lock(&chip->irq_lock);
  632. }
  633. static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
  634. {
  635. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  636. struct pca953x_chip *chip = gpiochip_get_data(gc);
  637. DECLARE_BITMAP(irq_mask, MAX_LINE);
  638. DECLARE_BITMAP(reg_direction, MAX_LINE);
  639. int level;
  640. if (chip->driver_data & PCA_PCAL) {
  641. /* Enable latch on interrupt-enabled inputs */
  642. pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
  643. bitmap_complement(irq_mask, chip->irq_mask, gc->ngpio);
  644. /* Unmask enabled interrupts */
  645. pca953x_write_regs(chip, PCAL953X_INT_MASK, irq_mask);
  646. }
  647. /* Switch direction to input if needed */
  648. pca953x_read_regs(chip, chip->regs->direction, reg_direction);
  649. bitmap_or(irq_mask, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio);
  650. bitmap_complement(reg_direction, reg_direction, gc->ngpio);
  651. bitmap_and(irq_mask, irq_mask, reg_direction, gc->ngpio);
  652. /* Look for any newly setup interrupt */
  653. for_each_set_bit(level, irq_mask, gc->ngpio)
  654. pca953x_gpio_direction_input(&chip->gpio_chip, level);
  655. mutex_unlock(&chip->irq_lock);
  656. }
  657. static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
  658. {
  659. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  660. struct pca953x_chip *chip = gpiochip_get_data(gc);
  661. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  662. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  663. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  664. d->irq, type);
  665. return -EINVAL;
  666. }
  667. assign_bit(hwirq, chip->irq_trig_fall, type & IRQ_TYPE_EDGE_FALLING);
  668. assign_bit(hwirq, chip->irq_trig_raise, type & IRQ_TYPE_EDGE_RISING);
  669. return 0;
  670. }
  671. static void pca953x_irq_shutdown(struct irq_data *d)
  672. {
  673. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  674. struct pca953x_chip *chip = gpiochip_get_data(gc);
  675. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  676. clear_bit(hwirq, chip->irq_trig_raise);
  677. clear_bit(hwirq, chip->irq_trig_fall);
  678. }
  679. static void pca953x_irq_print_chip(struct irq_data *data, struct seq_file *p)
  680. {
  681. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  682. seq_printf(p, dev_name(gc->parent));
  683. }
  684. static const struct irq_chip pca953x_irq_chip = {
  685. .irq_mask = pca953x_irq_mask,
  686. .irq_unmask = pca953x_irq_unmask,
  687. .irq_set_wake = pca953x_irq_set_wake,
  688. .irq_bus_lock = pca953x_irq_bus_lock,
  689. .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
  690. .irq_set_type = pca953x_irq_set_type,
  691. .irq_shutdown = pca953x_irq_shutdown,
  692. .irq_print_chip = pca953x_irq_print_chip,
  693. .flags = IRQCHIP_IMMUTABLE,
  694. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  695. };
  696. static bool pca953x_irq_pending(struct pca953x_chip *chip, unsigned long *pending)
  697. {
  698. struct gpio_chip *gc = &chip->gpio_chip;
  699. DECLARE_BITMAP(reg_direction, MAX_LINE);
  700. DECLARE_BITMAP(old_stat, MAX_LINE);
  701. DECLARE_BITMAP(cur_stat, MAX_LINE);
  702. DECLARE_BITMAP(new_stat, MAX_LINE);
  703. DECLARE_BITMAP(trigger, MAX_LINE);
  704. int ret;
  705. if (chip->driver_data & PCA_PCAL) {
  706. /* Read the current interrupt status from the device */
  707. ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
  708. if (ret)
  709. return false;
  710. /* Check latched inputs and clear interrupt status */
  711. ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
  712. if (ret)
  713. return false;
  714. /* Apply filter for rising/falling edge selection */
  715. bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio);
  716. bitmap_and(pending, new_stat, trigger, gc->ngpio);
  717. return !bitmap_empty(pending, gc->ngpio);
  718. }
  719. ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
  720. if (ret)
  721. return false;
  722. /* Remove output pins from the equation */
  723. pca953x_read_regs(chip, chip->regs->direction, reg_direction);
  724. bitmap_copy(old_stat, chip->irq_stat, gc->ngpio);
  725. bitmap_and(new_stat, cur_stat, reg_direction, gc->ngpio);
  726. bitmap_xor(cur_stat, new_stat, old_stat, gc->ngpio);
  727. bitmap_and(trigger, cur_stat, chip->irq_mask, gc->ngpio);
  728. bitmap_copy(chip->irq_stat, new_stat, gc->ngpio);
  729. if (bitmap_empty(trigger, gc->ngpio))
  730. return false;
  731. bitmap_and(cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio);
  732. bitmap_and(old_stat, chip->irq_trig_raise, new_stat, gc->ngpio);
  733. bitmap_or(new_stat, old_stat, cur_stat, gc->ngpio);
  734. bitmap_and(pending, new_stat, trigger, gc->ngpio);
  735. return !bitmap_empty(pending, gc->ngpio);
  736. }
  737. static irqreturn_t pca953x_irq_handler(int irq, void *devid)
  738. {
  739. struct pca953x_chip *chip = devid;
  740. struct gpio_chip *gc = &chip->gpio_chip;
  741. DECLARE_BITMAP(pending, MAX_LINE);
  742. int level;
  743. bool ret;
  744. bitmap_zero(pending, MAX_LINE);
  745. mutex_lock(&chip->i2c_lock);
  746. ret = pca953x_irq_pending(chip, pending);
  747. mutex_unlock(&chip->i2c_lock);
  748. if (ret) {
  749. ret = 0;
  750. for_each_set_bit(level, pending, gc->ngpio) {
  751. int nested_irq = irq_find_mapping(gc->irq.domain, level);
  752. if (unlikely(nested_irq <= 0)) {
  753. dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
  754. continue;
  755. }
  756. handle_nested_irq(nested_irq);
  757. ret = 1;
  758. }
  759. }
  760. return IRQ_RETVAL(ret);
  761. }
  762. static int pca953x_irq_setup(struct pca953x_chip *chip, int irq_base)
  763. {
  764. struct i2c_client *client = chip->client;
  765. DECLARE_BITMAP(reg_direction, MAX_LINE);
  766. DECLARE_BITMAP(irq_stat, MAX_LINE);
  767. struct gpio_irq_chip *girq;
  768. int ret;
  769. if (dmi_first_match(pca953x_dmi_acpi_irq_info)) {
  770. ret = pca953x_acpi_get_irq(&client->dev);
  771. if (ret > 0)
  772. client->irq = ret;
  773. }
  774. if (!client->irq)
  775. return 0;
  776. if (irq_base == -1)
  777. return 0;
  778. if (!(chip->driver_data & PCA_INT))
  779. return 0;
  780. ret = pca953x_read_regs(chip, chip->regs->input, irq_stat);
  781. if (ret)
  782. return ret;
  783. /*
  784. * There is no way to know which GPIO line generated the
  785. * interrupt. We have to rely on the previous read for
  786. * this purpose.
  787. */
  788. pca953x_read_regs(chip, chip->regs->direction, reg_direction);
  789. bitmap_and(chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio);
  790. mutex_init(&chip->irq_lock);
  791. girq = &chip->gpio_chip.irq;
  792. gpio_irq_chip_set_chip(girq, &pca953x_irq_chip);
  793. /* This will let us handle the parent IRQ in the driver */
  794. girq->parent_handler = NULL;
  795. girq->num_parents = 0;
  796. girq->parents = NULL;
  797. girq->default_type = IRQ_TYPE_NONE;
  798. girq->handler = handle_simple_irq;
  799. girq->threaded = true;
  800. girq->first = irq_base; /* FIXME: get rid of this */
  801. ret = devm_request_threaded_irq(&client->dev, client->irq,
  802. NULL, pca953x_irq_handler,
  803. IRQF_ONESHOT | IRQF_SHARED,
  804. dev_name(&client->dev), chip);
  805. if (ret) {
  806. dev_err(&client->dev, "failed to request irq %d\n",
  807. client->irq);
  808. return ret;
  809. }
  810. return 0;
  811. }
  812. #else /* CONFIG_GPIO_PCA953X_IRQ */
  813. static int pca953x_irq_setup(struct pca953x_chip *chip,
  814. int irq_base)
  815. {
  816. struct i2c_client *client = chip->client;
  817. if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
  818. dev_warn(&client->dev, "interrupt support not compiled in\n");
  819. return 0;
  820. }
  821. #endif
  822. static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
  823. {
  824. DECLARE_BITMAP(val, MAX_LINE);
  825. u8 regaddr;
  826. int ret;
  827. regaddr = chip->recalc_addr(chip, chip->regs->output, 0);
  828. ret = regcache_sync_region(chip->regmap, regaddr,
  829. regaddr + NBANK(chip) - 1);
  830. if (ret)
  831. goto out;
  832. regaddr = chip->recalc_addr(chip, chip->regs->direction, 0);
  833. ret = regcache_sync_region(chip->regmap, regaddr,
  834. regaddr + NBANK(chip) - 1);
  835. if (ret)
  836. goto out;
  837. /* set platform specific polarity inversion */
  838. if (invert)
  839. bitmap_fill(val, MAX_LINE);
  840. else
  841. bitmap_zero(val, MAX_LINE);
  842. ret = pca953x_write_regs(chip, chip->regs->invert, val);
  843. out:
  844. return ret;
  845. }
  846. static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
  847. {
  848. DECLARE_BITMAP(val, MAX_LINE);
  849. unsigned int i;
  850. int ret;
  851. ret = device_pca95xx_init(chip, invert);
  852. if (ret)
  853. goto out;
  854. /* To enable register 6, 7 to control pull up and pull down */
  855. for (i = 0; i < NBANK(chip); i++)
  856. bitmap_set_value8(val, 0x02, i * BANK_SZ);
  857. ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
  858. if (ret)
  859. goto out;
  860. return 0;
  861. out:
  862. return ret;
  863. }
  864. static int pca953x_probe(struct i2c_client *client,
  865. const struct i2c_device_id *i2c_id)
  866. {
  867. struct pca953x_platform_data *pdata;
  868. struct pca953x_chip *chip;
  869. int irq_base = 0;
  870. int ret;
  871. u32 invert = 0;
  872. struct regulator *reg;
  873. const struct regmap_config *regmap_config;
  874. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  875. if (chip == NULL)
  876. return -ENOMEM;
  877. pdata = dev_get_platdata(&client->dev);
  878. if (pdata) {
  879. irq_base = pdata->irq_base;
  880. chip->gpio_start = pdata->gpio_base;
  881. invert = pdata->invert;
  882. chip->names = pdata->names;
  883. } else {
  884. struct gpio_desc *reset_gpio;
  885. chip->gpio_start = -1;
  886. irq_base = 0;
  887. /*
  888. * See if we need to de-assert a reset pin.
  889. *
  890. * There is no known ACPI-enabled platforms that are
  891. * using "reset" GPIO. Otherwise any of those platform
  892. * must use _DSD method with corresponding property.
  893. */
  894. reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  895. GPIOD_OUT_LOW);
  896. if (IS_ERR(reset_gpio))
  897. return PTR_ERR(reset_gpio);
  898. }
  899. chip->client = client;
  900. reg = devm_regulator_get(&client->dev, "vcc");
  901. if (IS_ERR(reg))
  902. return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n");
  903. ret = regulator_enable(reg);
  904. if (ret) {
  905. dev_err(&client->dev, "reg en err: %d\n", ret);
  906. return ret;
  907. }
  908. chip->regulator = reg;
  909. if (i2c_id) {
  910. chip->driver_data = i2c_id->driver_data;
  911. } else {
  912. const void *match;
  913. match = device_get_match_data(&client->dev);
  914. if (!match) {
  915. ret = -ENODEV;
  916. goto err_exit;
  917. }
  918. chip->driver_data = (uintptr_t)match;
  919. }
  920. i2c_set_clientdata(client, chip);
  921. pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
  922. if (NBANK(chip) > 2 || PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
  923. dev_info(&client->dev, "using AI\n");
  924. regmap_config = &pca953x_ai_i2c_regmap;
  925. } else {
  926. dev_info(&client->dev, "using no AI\n");
  927. regmap_config = &pca953x_i2c_regmap;
  928. }
  929. if (PCA_CHIP_TYPE(chip->driver_data) == PCAL653X_TYPE) {
  930. chip->recalc_addr = pcal6534_recalc_addr;
  931. chip->check_reg = pcal6534_check_register;
  932. } else {
  933. chip->recalc_addr = pca953x_recalc_addr;
  934. chip->check_reg = pca953x_check_register;
  935. }
  936. chip->regmap = devm_regmap_init_i2c(client, regmap_config);
  937. if (IS_ERR(chip->regmap)) {
  938. ret = PTR_ERR(chip->regmap);
  939. goto err_exit;
  940. }
  941. regcache_mark_dirty(chip->regmap);
  942. mutex_init(&chip->i2c_lock);
  943. /*
  944. * In case we have an i2c-mux controlled by a GPIO provided by an
  945. * expander using the same driver higher on the device tree, read the
  946. * i2c adapter nesting depth and use the retrieved value as lockdep
  947. * subclass for chip->i2c_lock.
  948. *
  949. * REVISIT: This solution is not complete. It protects us from lockdep
  950. * false positives when the expander controlling the i2c-mux is on
  951. * a different level on the device tree, but not when it's on the same
  952. * level on a different branch (in which case the subclass number
  953. * would be the same).
  954. *
  955. * TODO: Once a correct solution is developed, a similar fix should be
  956. * applied to all other i2c-controlled GPIO expanders (and potentially
  957. * regmap-i2c).
  958. */
  959. lockdep_set_subclass(&chip->i2c_lock,
  960. i2c_adapter_depth(client->adapter));
  961. /* initialize cached registers from their original values.
  962. * we can't share this chip with another i2c master.
  963. */
  964. if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) {
  965. chip->regs = &pca957x_regs;
  966. ret = device_pca957x_init(chip, invert);
  967. } else {
  968. chip->regs = &pca953x_regs;
  969. ret = device_pca95xx_init(chip, invert);
  970. }
  971. if (ret)
  972. goto err_exit;
  973. ret = pca953x_irq_setup(chip, irq_base);
  974. if (ret)
  975. goto err_exit;
  976. ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
  977. if (ret)
  978. goto err_exit;
  979. if (pdata && pdata->setup) {
  980. ret = pdata->setup(client, chip->gpio_chip.base,
  981. chip->gpio_chip.ngpio, pdata->context);
  982. if (ret < 0)
  983. dev_warn(&client->dev, "setup failed, %d\n", ret);
  984. }
  985. return 0;
  986. err_exit:
  987. regulator_disable(chip->regulator);
  988. return ret;
  989. }
  990. static void pca953x_remove(struct i2c_client *client)
  991. {
  992. struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
  993. struct pca953x_chip *chip = i2c_get_clientdata(client);
  994. if (pdata && pdata->teardown) {
  995. pdata->teardown(client, chip->gpio_chip.base,
  996. chip->gpio_chip.ngpio, pdata->context);
  997. }
  998. regulator_disable(chip->regulator);
  999. }
  1000. #ifdef CONFIG_PM_SLEEP
  1001. static int pca953x_regcache_sync(struct device *dev)
  1002. {
  1003. struct pca953x_chip *chip = dev_get_drvdata(dev);
  1004. int ret;
  1005. u8 regaddr;
  1006. /*
  1007. * The ordering between direction and output is important,
  1008. * sync these registers first and only then sync the rest.
  1009. */
  1010. regaddr = chip->recalc_addr(chip, chip->regs->direction, 0);
  1011. ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
  1012. if (ret) {
  1013. dev_err(dev, "Failed to sync GPIO dir registers: %d\n", ret);
  1014. return ret;
  1015. }
  1016. regaddr = chip->recalc_addr(chip, chip->regs->output, 0);
  1017. ret = regcache_sync_region(chip->regmap, regaddr, regaddr + NBANK(chip) - 1);
  1018. if (ret) {
  1019. dev_err(dev, "Failed to sync GPIO out registers: %d\n", ret);
  1020. return ret;
  1021. }
  1022. #ifdef CONFIG_GPIO_PCA953X_IRQ
  1023. if (chip->driver_data & PCA_PCAL) {
  1024. regaddr = chip->recalc_addr(chip, PCAL953X_IN_LATCH, 0);
  1025. ret = regcache_sync_region(chip->regmap, regaddr,
  1026. regaddr + NBANK(chip) - 1);
  1027. if (ret) {
  1028. dev_err(dev, "Failed to sync INT latch registers: %d\n",
  1029. ret);
  1030. return ret;
  1031. }
  1032. regaddr = chip->recalc_addr(chip, PCAL953X_INT_MASK, 0);
  1033. ret = regcache_sync_region(chip->regmap, regaddr,
  1034. regaddr + NBANK(chip) - 1);
  1035. if (ret) {
  1036. dev_err(dev, "Failed to sync INT mask registers: %d\n",
  1037. ret);
  1038. return ret;
  1039. }
  1040. }
  1041. #endif
  1042. return 0;
  1043. }
  1044. static int pca953x_suspend(struct device *dev)
  1045. {
  1046. struct pca953x_chip *chip = dev_get_drvdata(dev);
  1047. mutex_lock(&chip->i2c_lock);
  1048. regcache_cache_only(chip->regmap, true);
  1049. mutex_unlock(&chip->i2c_lock);
  1050. if (atomic_read(&chip->wakeup_path))
  1051. device_set_wakeup_path(dev);
  1052. else
  1053. regulator_disable(chip->regulator);
  1054. return 0;
  1055. }
  1056. static int pca953x_resume(struct device *dev)
  1057. {
  1058. struct pca953x_chip *chip = dev_get_drvdata(dev);
  1059. int ret;
  1060. if (!atomic_read(&chip->wakeup_path)) {
  1061. ret = regulator_enable(chip->regulator);
  1062. if (ret) {
  1063. dev_err(dev, "Failed to enable regulator: %d\n", ret);
  1064. return 0;
  1065. }
  1066. }
  1067. mutex_lock(&chip->i2c_lock);
  1068. regcache_cache_only(chip->regmap, false);
  1069. regcache_mark_dirty(chip->regmap);
  1070. ret = pca953x_regcache_sync(dev);
  1071. if (ret) {
  1072. mutex_unlock(&chip->i2c_lock);
  1073. return ret;
  1074. }
  1075. ret = regcache_sync(chip->regmap);
  1076. mutex_unlock(&chip->i2c_lock);
  1077. if (ret) {
  1078. dev_err(dev, "Failed to restore register map: %d\n", ret);
  1079. return ret;
  1080. }
  1081. return 0;
  1082. }
  1083. #endif
  1084. /* convenience to stop overlong match-table lines */
  1085. #define OF_653X(__nrgpio, __int) ((void *)(__nrgpio | PCAL653X_TYPE | __int))
  1086. #define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
  1087. #define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
  1088. static const struct of_device_id pca953x_dt_ids[] = {
  1089. { .compatible = "nxp,pca6408", .data = OF_953X(8, PCA_INT), },
  1090. { .compatible = "nxp,pca6416", .data = OF_953X(16, PCA_INT), },
  1091. { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
  1092. { .compatible = "nxp,pca9506", .data = OF_953X(40, PCA_INT), },
  1093. { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
  1094. { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
  1095. { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
  1096. { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
  1097. { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
  1098. { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
  1099. { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
  1100. { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
  1101. { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
  1102. { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
  1103. { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
  1104. { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
  1105. { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
  1106. { .compatible = "nxp,pcal6408", .data = OF_953X(8, PCA_LATCH_INT), },
  1107. { .compatible = "nxp,pcal6416", .data = OF_953X(16, PCA_LATCH_INT), },
  1108. { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
  1109. { .compatible = "nxp,pcal6534", .data = OF_653X(34, PCA_LATCH_INT), },
  1110. { .compatible = "nxp,pcal9535", .data = OF_953X(16, PCA_LATCH_INT), },
  1111. { .compatible = "nxp,pcal9554b", .data = OF_953X( 8, PCA_LATCH_INT), },
  1112. { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
  1113. { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
  1114. { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
  1115. { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
  1116. { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
  1117. { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
  1118. { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
  1119. { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
  1120. { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
  1121. { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
  1122. { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
  1123. { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
  1124. { .compatible = "onnn,cat9554", .data = OF_953X( 8, PCA_INT), },
  1125. { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
  1126. { .compatible = "onnn,pca9655", .data = OF_953X(16, PCA_INT), },
  1127. { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
  1128. { }
  1129. };
  1130. MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
  1131. static SIMPLE_DEV_PM_OPS(pca953x_pm_ops, pca953x_suspend, pca953x_resume);
  1132. static struct i2c_driver pca953x_driver = {
  1133. .driver = {
  1134. .name = "pca953x",
  1135. .pm = &pca953x_pm_ops,
  1136. .of_match_table = pca953x_dt_ids,
  1137. .acpi_match_table = pca953x_acpi_ids,
  1138. },
  1139. .probe = pca953x_probe,
  1140. .remove = pca953x_remove,
  1141. .id_table = pca953x_id,
  1142. };
  1143. static int __init pca953x_init(void)
  1144. {
  1145. return i2c_add_driver(&pca953x_driver);
  1146. }
  1147. /* register after i2c postcore initcall and before
  1148. * subsys initcalls that may rely on these GPIOs
  1149. */
  1150. subsys_initcall(pca953x_init);
  1151. static void __exit pca953x_exit(void)
  1152. {
  1153. i2c_del_driver(&pca953x_driver);
  1154. }
  1155. module_exit(pca953x_exit);
  1156. MODULE_AUTHOR("eric miao <[email protected]>");
  1157. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  1158. MODULE_LICENSE("GPL");