adp5588-gpio.c 5.9 KB

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