gpio-adp5588.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * GPIO Chip driver for Analog Devices
  3. * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
  4. *
  5. * Copyright 2009-2010 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/i2c.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/platform_data/adp5588.h>
  18. #define DRV_NAME "adp5588-gpio"
  19. /*
  20. * Early pre 4.0 Silicon required to delay readout by at least 25ms,
  21. * since the Event Counter Register updated 25ms after the interrupt
  22. * asserted.
  23. */
  24. #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
  25. struct adp5588_gpio {
  26. struct i2c_client *client;
  27. struct gpio_chip gpio_chip;
  28. struct mutex lock; /* protect cached dir, dat_out */
  29. /* protect serialized access to the interrupt controller bus */
  30. struct mutex irq_lock;
  31. unsigned gpio_start;
  32. unsigned irq_base;
  33. uint8_t dat_out[3];
  34. uint8_t dir[3];
  35. uint8_t int_lvl[3];
  36. uint8_t int_en[3];
  37. uint8_t irq_mask[3];
  38. uint8_t irq_stat[3];
  39. uint8_t int_input_en[3];
  40. uint8_t int_lvl_cached[3];
  41. };
  42. static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
  43. {
  44. int ret = i2c_smbus_read_byte_data(client, reg);
  45. if (ret < 0)
  46. dev_err(&client->dev, "Read Error\n");
  47. return ret;
  48. }
  49. static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
  50. {
  51. int ret = i2c_smbus_write_byte_data(client, reg, val);
  52. if (ret < 0)
  53. dev_err(&client->dev, "Write Error\n");
  54. return ret;
  55. }
  56. static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
  57. {
  58. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  59. unsigned bank = ADP5588_BANK(off);
  60. unsigned bit = ADP5588_BIT(off);
  61. int val;
  62. mutex_lock(&dev->lock);
  63. if (dev->dir[bank] & bit)
  64. val = dev->dat_out[bank];
  65. else
  66. val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
  67. mutex_unlock(&dev->lock);
  68. return !!(val & bit);
  69. }
  70. static void adp5588_gpio_set_value(struct gpio_chip *chip,
  71. unsigned off, int val)
  72. {
  73. unsigned bank, bit;
  74. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  75. bank = ADP5588_BANK(off);
  76. bit = ADP5588_BIT(off);
  77. mutex_lock(&dev->lock);
  78. if (val)
  79. dev->dat_out[bank] |= bit;
  80. else
  81. dev->dat_out[bank] &= ~bit;
  82. adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  83. dev->dat_out[bank]);
  84. mutex_unlock(&dev->lock);
  85. }
  86. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  87. {
  88. int ret;
  89. unsigned bank;
  90. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  91. bank = ADP5588_BANK(off);
  92. mutex_lock(&dev->lock);
  93. dev->dir[bank] &= ~ADP5588_BIT(off);
  94. ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
  95. mutex_unlock(&dev->lock);
  96. return ret;
  97. }
  98. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  99. unsigned off, int val)
  100. {
  101. int ret;
  102. unsigned bank, bit;
  103. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  104. bank = ADP5588_BANK(off);
  105. bit = ADP5588_BIT(off);
  106. mutex_lock(&dev->lock);
  107. dev->dir[bank] |= bit;
  108. if (val)
  109. dev->dat_out[bank] |= bit;
  110. else
  111. dev->dat_out[bank] &= ~bit;
  112. ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  113. dev->dat_out[bank]);
  114. ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
  115. dev->dir[bank]);
  116. mutex_unlock(&dev->lock);
  117. return ret;
  118. }
  119. #ifdef CONFIG_GPIO_ADP5588_IRQ
  120. static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
  121. {
  122. struct adp5588_gpio *dev = gpiochip_get_data(chip);
  123. return dev->irq_base + off;
  124. }
  125. static void adp5588_irq_bus_lock(struct irq_data *d)
  126. {
  127. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  128. mutex_lock(&dev->irq_lock);
  129. }
  130. /*
  131. * genirq core code can issue chip->mask/unmask from atomic context.
  132. * This doesn't work for slow busses where an access needs to sleep.
  133. * bus_sync_unlock() is therefore called outside the atomic context,
  134. * syncs the current irq mask state with the slow external controller
  135. * and unlocks the bus.
  136. */
  137. static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
  138. {
  139. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  140. int i;
  141. for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  142. if (dev->int_input_en[i]) {
  143. mutex_lock(&dev->lock);
  144. dev->dir[i] &= ~dev->int_input_en[i];
  145. dev->int_input_en[i] = 0;
  146. adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
  147. dev->dir[i]);
  148. mutex_unlock(&dev->lock);
  149. }
  150. if (dev->int_lvl_cached[i] != dev->int_lvl[i]) {
  151. dev->int_lvl_cached[i] = dev->int_lvl[i];
  152. adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + i,
  153. dev->int_lvl[i]);
  154. }
  155. if (dev->int_en[i] ^ dev->irq_mask[i]) {
  156. dev->int_en[i] = dev->irq_mask[i];
  157. adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
  158. dev->int_en[i]);
  159. }
  160. }
  161. mutex_unlock(&dev->irq_lock);
  162. }
  163. static void adp5588_irq_mask(struct irq_data *d)
  164. {
  165. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  166. unsigned gpio = d->irq - dev->irq_base;
  167. dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
  168. }
  169. static void adp5588_irq_unmask(struct irq_data *d)
  170. {
  171. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  172. unsigned gpio = d->irq - dev->irq_base;
  173. dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
  174. }
  175. static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
  176. {
  177. struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
  178. uint16_t gpio = d->irq - dev->irq_base;
  179. unsigned bank, bit;
  180. if ((type & IRQ_TYPE_EDGE_BOTH)) {
  181. dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
  182. d->irq, type);
  183. return -EINVAL;
  184. }
  185. bank = ADP5588_BANK(gpio);
  186. bit = ADP5588_BIT(gpio);
  187. if (type & IRQ_TYPE_LEVEL_HIGH)
  188. dev->int_lvl[bank] |= bit;
  189. else if (type & IRQ_TYPE_LEVEL_LOW)
  190. dev->int_lvl[bank] &= ~bit;
  191. else
  192. return -EINVAL;
  193. dev->int_input_en[bank] |= bit;
  194. return 0;
  195. }
  196. static struct irq_chip adp5588_irq_chip = {
  197. .name = "adp5588",
  198. .irq_mask = adp5588_irq_mask,
  199. .irq_unmask = adp5588_irq_unmask,
  200. .irq_bus_lock = adp5588_irq_bus_lock,
  201. .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
  202. .irq_set_type = adp5588_irq_set_type,
  203. };
  204. static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
  205. {
  206. int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
  207. if (ret < 0)
  208. dev_err(&client->dev, "Read INT_STAT Error\n");
  209. return ret;
  210. }
  211. static irqreturn_t adp5588_irq_handler(int irq, void *devid)
  212. {
  213. struct adp5588_gpio *dev = devid;
  214. unsigned status, bank, bit, pending;
  215. int ret;
  216. status = adp5588_gpio_read(dev->client, INT_STAT);
  217. if (status & ADP5588_GPI_INT) {
  218. ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
  219. if (ret < 0)
  220. memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
  221. for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
  222. bank++, bit = 0) {
  223. pending = dev->irq_stat[bank] & dev->irq_mask[bank];
  224. while (pending) {
  225. if (pending & (1 << bit)) {
  226. handle_nested_irq(dev->irq_base +
  227. (bank << 3) + bit);
  228. pending &= ~(1 << bit);
  229. }
  230. bit++;
  231. }
  232. }
  233. }
  234. adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
  235. return IRQ_HANDLED;
  236. }
  237. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  238. {
  239. struct i2c_client *client = dev->client;
  240. struct adp5588_gpio_platform_data *pdata =
  241. dev_get_platdata(&client->dev);
  242. unsigned gpio;
  243. int ret;
  244. adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
  245. adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
  246. adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
  247. dev->irq_base = pdata->irq_base;
  248. mutex_init(&dev->irq_lock);
  249. for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
  250. int irq = gpio + dev->irq_base;
  251. irq_set_chip_data(irq, dev);
  252. irq_set_chip_and_handler(irq, &adp5588_irq_chip,
  253. handle_level_irq);
  254. irq_set_nested_thread(irq, 1);
  255. irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
  256. }
  257. ret = request_threaded_irq(client->irq,
  258. NULL,
  259. adp5588_irq_handler,
  260. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  261. dev_name(&client->dev), dev);
  262. if (ret) {
  263. dev_err(&client->dev, "failed to request irq %d\n",
  264. client->irq);
  265. goto out;
  266. }
  267. dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
  268. adp5588_gpio_write(client, CFG,
  269. ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
  270. return 0;
  271. out:
  272. dev->irq_base = 0;
  273. return ret;
  274. }
  275. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  276. {
  277. if (dev->irq_base)
  278. free_irq(dev->client->irq, dev);
  279. }
  280. #else
  281. static int adp5588_irq_setup(struct adp5588_gpio *dev)
  282. {
  283. struct i2c_client *client = dev->client;
  284. dev_warn(&client->dev, "interrupt support not compiled in\n");
  285. return 0;
  286. }
  287. static void adp5588_irq_teardown(struct adp5588_gpio *dev)
  288. {
  289. }
  290. #endif /* CONFIG_GPIO_ADP5588_IRQ */
  291. static int adp5588_gpio_probe(struct i2c_client *client,
  292. const struct i2c_device_id *id)
  293. {
  294. struct adp5588_gpio_platform_data *pdata =
  295. dev_get_platdata(&client->dev);
  296. struct adp5588_gpio *dev;
  297. struct gpio_chip *gc;
  298. int ret, i, revid;
  299. if (!pdata) {
  300. dev_err(&client->dev, "missing platform data\n");
  301. return -ENODEV;
  302. }
  303. if (!i2c_check_functionality(client->adapter,
  304. I2C_FUNC_SMBUS_BYTE_DATA)) {
  305. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  306. return -EIO;
  307. }
  308. dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
  309. if (!dev)
  310. return -ENOMEM;
  311. dev->client = client;
  312. gc = &dev->gpio_chip;
  313. gc->direction_input = adp5588_gpio_direction_input;
  314. gc->direction_output = adp5588_gpio_direction_output;
  315. gc->get = adp5588_gpio_get_value;
  316. gc->set = adp5588_gpio_set_value;
  317. gc->can_sleep = true;
  318. gc->base = pdata->gpio_start;
  319. gc->ngpio = ADP5588_MAXGPIO;
  320. gc->label = client->name;
  321. gc->owner = THIS_MODULE;
  322. gc->names = pdata->names;
  323. mutex_init(&dev->lock);
  324. ret = adp5588_gpio_read(dev->client, DEV_ID);
  325. if (ret < 0)
  326. goto err;
  327. revid = ret & ADP5588_DEVICE_ID_MASK;
  328. for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
  329. dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
  330. dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
  331. ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
  332. ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
  333. (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
  334. ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
  335. if (ret)
  336. goto err;
  337. }
  338. if (pdata->irq_base) {
  339. if (WA_DELAYED_READOUT_REVID(revid)) {
  340. dev_warn(&client->dev, "GPIO int not supported\n");
  341. } else {
  342. ret = adp5588_irq_setup(dev);
  343. if (ret)
  344. goto err;
  345. }
  346. }
  347. ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
  348. if (ret)
  349. goto err_irq;
  350. dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
  351. pdata->irq_base, revid);
  352. if (pdata->setup) {
  353. ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
  354. if (ret < 0)
  355. dev_warn(&client->dev, "setup failed, %d\n", ret);
  356. }
  357. i2c_set_clientdata(client, dev);
  358. return 0;
  359. err_irq:
  360. adp5588_irq_teardown(dev);
  361. err:
  362. return ret;
  363. }
  364. static int adp5588_gpio_remove(struct i2c_client *client)
  365. {
  366. struct adp5588_gpio_platform_data *pdata =
  367. dev_get_platdata(&client->dev);
  368. struct adp5588_gpio *dev = i2c_get_clientdata(client);
  369. int ret;
  370. if (pdata->teardown) {
  371. ret = pdata->teardown(client,
  372. dev->gpio_chip.base, dev->gpio_chip.ngpio,
  373. pdata->context);
  374. if (ret < 0) {
  375. dev_err(&client->dev, "teardown failed %d\n", ret);
  376. return ret;
  377. }
  378. }
  379. if (dev->irq_base)
  380. free_irq(dev->client->irq, dev);
  381. return 0;
  382. }
  383. static const struct i2c_device_id adp5588_gpio_id[] = {
  384. {DRV_NAME, 0},
  385. {}
  386. };
  387. MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
  388. static struct i2c_driver adp5588_gpio_driver = {
  389. .driver = {
  390. .name = DRV_NAME,
  391. },
  392. .probe = adp5588_gpio_probe,
  393. .remove = adp5588_gpio_remove,
  394. .id_table = adp5588_gpio_id,
  395. };
  396. module_i2c_driver(adp5588_gpio_driver);
  397. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  398. MODULE_DESCRIPTION("GPIO ADP5588 Driver");
  399. MODULE_LICENSE("GPL");