adp5588-gpio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * GPIO Chip driver for Analog Devices
  3. * ADP5588 I/O Expander and QWERTY Keypad Controller
  4. *
  5. * Copyright 2009 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/i2c/adp5588.h>
  16. #define DRV_NAME "adp5588-gpio"
  17. #define MAXGPIO 18
  18. #define ADP_BANK(offs) ((offs) >> 3)
  19. #define ADP_BIT(offs) (1u << ((offs) & 0x7))
  20. struct adp5588_gpio {
  21. struct i2c_client *client;
  22. struct gpio_chip gpio_chip;
  23. struct mutex lock; /* protect cached dir, dat_out */
  24. unsigned gpio_start;
  25. uint8_t dat_out[3];
  26. uint8_t dir[3];
  27. };
  28. static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
  29. {
  30. int ret = i2c_smbus_read_byte_data(client, reg);
  31. if (ret < 0)
  32. dev_err(&client->dev, "Read Error\n");
  33. return ret;
  34. }
  35. static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
  36. {
  37. int ret = i2c_smbus_write_byte_data(client, reg, val);
  38. if (ret < 0)
  39. dev_err(&client->dev, "Write Error\n");
  40. return ret;
  41. }
  42. static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
  43. {
  44. struct adp5588_gpio *dev =
  45. container_of(chip, struct adp5588_gpio, gpio_chip);
  46. return !!(adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + ADP_BANK(off))
  47. & ADP_BIT(off));
  48. }
  49. static void adp5588_gpio_set_value(struct gpio_chip *chip,
  50. unsigned off, int val)
  51. {
  52. unsigned bank, bit;
  53. struct adp5588_gpio *dev =
  54. container_of(chip, struct adp5588_gpio, gpio_chip);
  55. bank = ADP_BANK(off);
  56. bit = ADP_BIT(off);
  57. mutex_lock(&dev->lock);
  58. if (val)
  59. dev->dat_out[bank] |= bit;
  60. else
  61. dev->dat_out[bank] &= ~bit;
  62. adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  63. dev->dat_out[bank]);
  64. mutex_unlock(&dev->lock);
  65. }
  66. static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
  67. {
  68. int ret;
  69. unsigned bank;
  70. struct adp5588_gpio *dev =
  71. container_of(chip, struct adp5588_gpio, gpio_chip);
  72. bank = ADP_BANK(off);
  73. mutex_lock(&dev->lock);
  74. dev->dir[bank] &= ~ADP_BIT(off);
  75. ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
  76. mutex_unlock(&dev->lock);
  77. return ret;
  78. }
  79. static int adp5588_gpio_direction_output(struct gpio_chip *chip,
  80. unsigned off, int val)
  81. {
  82. int ret;
  83. unsigned bank, bit;
  84. struct adp5588_gpio *dev =
  85. container_of(chip, struct adp5588_gpio, gpio_chip);
  86. bank = ADP_BANK(off);
  87. bit = ADP_BIT(off);
  88. mutex_lock(&dev->lock);
  89. dev->dir[bank] |= bit;
  90. if (val)
  91. dev->dat_out[bank] |= bit;
  92. else
  93. dev->dat_out[bank] &= ~bit;
  94. ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
  95. dev->dat_out[bank]);
  96. ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
  97. dev->dir[bank]);
  98. mutex_unlock(&dev->lock);
  99. return ret;
  100. }
  101. static int __devinit adp5588_gpio_probe(struct i2c_client *client,
  102. const struct i2c_device_id *id)
  103. {
  104. struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
  105. struct adp5588_gpio *dev;
  106. struct gpio_chip *gc;
  107. int ret, i, revid;
  108. if (pdata == NULL) {
  109. dev_err(&client->dev, "missing platform data\n");
  110. return -ENODEV;
  111. }
  112. if (!i2c_check_functionality(client->adapter,
  113. I2C_FUNC_SMBUS_BYTE_DATA)) {
  114. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  115. return -EIO;
  116. }
  117. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  118. if (dev == NULL) {
  119. dev_err(&client->dev, "failed to alloc memory\n");
  120. return -ENOMEM;
  121. }
  122. dev->client = client;
  123. gc = &dev->gpio_chip;
  124. gc->direction_input = adp5588_gpio_direction_input;
  125. gc->direction_output = adp5588_gpio_direction_output;
  126. gc->get = adp5588_gpio_get_value;
  127. gc->set = adp5588_gpio_set_value;
  128. gc->can_sleep = 1;
  129. gc->base = pdata->gpio_start;
  130. gc->ngpio = MAXGPIO;
  131. gc->label = client->name;
  132. gc->owner = THIS_MODULE;
  133. mutex_init(&dev->lock);
  134. ret = adp5588_gpio_read(dev->client, DEV_ID);
  135. if (ret < 0)
  136. goto err;
  137. revid = ret & ADP5588_DEVICE_ID_MASK;
  138. for (i = 0, ret = 0; i <= ADP_BANK(MAXGPIO); i++) {
  139. dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
  140. dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
  141. ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
  142. ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
  143. (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
  144. if (ret)
  145. goto err;
  146. }
  147. ret = gpiochip_add(&dev->gpio_chip);
  148. if (ret)
  149. goto err;
  150. dev_info(&client->dev, "gpios %d..%d on a %s Rev. %d\n",
  151. gc->base, gc->base + gc->ngpio - 1,
  152. client->name, revid);
  153. if (pdata->setup) {
  154. ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
  155. if (ret < 0)
  156. dev_warn(&client->dev, "setup failed, %d\n", ret);
  157. }
  158. i2c_set_clientdata(client, dev);
  159. return 0;
  160. err:
  161. kfree(dev);
  162. return ret;
  163. }
  164. static int __devexit adp5588_gpio_remove(struct i2c_client *client)
  165. {
  166. struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
  167. struct adp5588_gpio *dev = i2c_get_clientdata(client);
  168. int ret;
  169. if (pdata->teardown) {
  170. ret = pdata->teardown(client,
  171. dev->gpio_chip.base, dev->gpio_chip.ngpio,
  172. pdata->context);
  173. if (ret < 0) {
  174. dev_err(&client->dev, "teardown failed %d\n", ret);
  175. return ret;
  176. }
  177. }
  178. ret = gpiochip_remove(&dev->gpio_chip);
  179. if (ret) {
  180. dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
  181. return ret;
  182. }
  183. kfree(dev);
  184. return 0;
  185. }
  186. static const struct i2c_device_id adp5588_gpio_id[] = {
  187. {DRV_NAME, 0},
  188. {}
  189. };
  190. MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
  191. static struct i2c_driver adp5588_gpio_driver = {
  192. .driver = {
  193. .name = DRV_NAME,
  194. },
  195. .probe = adp5588_gpio_probe,
  196. .remove = __devexit_p(adp5588_gpio_remove),
  197. .id_table = adp5588_gpio_id,
  198. };
  199. static int __init adp5588_gpio_init(void)
  200. {
  201. return i2c_add_driver(&adp5588_gpio_driver);
  202. }
  203. module_init(adp5588_gpio_init);
  204. static void __exit adp5588_gpio_exit(void)
  205. {
  206. i2c_del_driver(&adp5588_gpio_driver);
  207. }
  208. module_exit(adp5588_gpio_exit);
  209. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  210. MODULE_DESCRIPTION("GPIO ADP5588 Driver");
  211. MODULE_LICENSE("GPL");