gpio-adp5588.c 12 KB

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