gpio-da9052.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * GPIO Driver for Dialog DA9052 PMICs.
  3. *
  4. * Copyright(c) 2011 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/gpio.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/mfd/da9052/da9052.h>
  22. #include <linux/mfd/da9052/reg.h>
  23. #include <linux/mfd/da9052/pdata.h>
  24. #include <linux/mfd/da9052/gpio.h>
  25. #define DA9052_INPUT 1
  26. #define DA9052_OUTPUT_OPENDRAIN 2
  27. #define DA9052_OUTPUT_PUSHPULL 3
  28. #define DA9052_SUPPLY_VDD_IO1 0
  29. #define DA9052_DEBOUNCING_OFF 0
  30. #define DA9052_DEBOUNCING_ON 1
  31. #define DA9052_OUTPUT_LOWLEVEL 0
  32. #define DA9052_ACTIVE_LOW 0
  33. #define DA9052_ACTIVE_HIGH 1
  34. #define DA9052_GPIO_MAX_PORTS_PER_REGISTER 8
  35. #define DA9052_GPIO_SHIFT_COUNT(no) (no%8)
  36. #define DA9052_GPIO_MASK_UPPER_NIBBLE 0xF0
  37. #define DA9052_GPIO_MASK_LOWER_NIBBLE 0x0F
  38. #define DA9052_GPIO_NIBBLE_SHIFT 4
  39. struct da9052_gpio {
  40. struct da9052 *da9052;
  41. struct gpio_chip gp;
  42. };
  43. static inline struct da9052_gpio *to_da9052_gpio(struct gpio_chip *chip)
  44. {
  45. return container_of(chip, struct da9052_gpio, gp);
  46. }
  47. static unsigned char da9052_gpio_port_odd(unsigned offset)
  48. {
  49. return offset % 2;
  50. }
  51. static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
  52. {
  53. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  54. int da9052_port_direction = 0;
  55. int ret;
  56. ret = da9052_reg_read(gpio->da9052,
  57. DA9052_GPIO_0_1_REG + (offset >> 1));
  58. if (ret < 0)
  59. return ret;
  60. if (da9052_gpio_port_odd(offset)) {
  61. da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
  62. da9052_port_direction >>= 4;
  63. } else {
  64. da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
  65. }
  66. switch (da9052_port_direction) {
  67. case DA9052_INPUT:
  68. if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
  69. ret = da9052_reg_read(gpio->da9052,
  70. DA9052_STATUS_C_REG);
  71. else
  72. ret = da9052_reg_read(gpio->da9052,
  73. DA9052_STATUS_D_REG);
  74. if (ret < 0)
  75. return ret;
  76. if (ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)))
  77. return 1;
  78. else
  79. return 0;
  80. case DA9052_OUTPUT_PUSHPULL:
  81. if (da9052_gpio_port_odd(offset))
  82. return ret & DA9052_GPIO_ODD_PORT_MODE;
  83. else
  84. return ret & DA9052_GPIO_EVEN_PORT_MODE;
  85. default:
  86. return -EINVAL;
  87. }
  88. }
  89. static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  90. {
  91. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  92. unsigned char register_value = 0;
  93. int ret;
  94. if (da9052_gpio_port_odd(offset)) {
  95. if (value) {
  96. register_value = DA9052_GPIO_ODD_PORT_MODE;
  97. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  98. DA9052_GPIO_0_1_REG,
  99. DA9052_GPIO_ODD_PORT_MODE,
  100. register_value);
  101. if (ret != 0)
  102. dev_err(gpio->da9052->dev,
  103. "Failed to updated gpio odd reg,%d",
  104. ret);
  105. }
  106. } else {
  107. if (value) {
  108. register_value = DA9052_GPIO_EVEN_PORT_MODE;
  109. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  110. DA9052_GPIO_0_1_REG,
  111. DA9052_GPIO_EVEN_PORT_MODE,
  112. register_value);
  113. if (ret != 0)
  114. dev_err(gpio->da9052->dev,
  115. "Failed to updated gpio even reg,%d",
  116. ret);
  117. }
  118. }
  119. }
  120. static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  121. {
  122. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  123. unsigned char register_value;
  124. int ret;
  125. /* Format: function - 2 bits type - 1 bit mode - 1 bit */
  126. register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
  127. DA9052_DEBOUNCING_ON << 3;
  128. if (da9052_gpio_port_odd(offset))
  129. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  130. DA9052_GPIO_0_1_REG,
  131. DA9052_GPIO_MASK_UPPER_NIBBLE,
  132. (register_value <<
  133. DA9052_GPIO_NIBBLE_SHIFT));
  134. else
  135. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  136. DA9052_GPIO_0_1_REG,
  137. DA9052_GPIO_MASK_LOWER_NIBBLE,
  138. register_value);
  139. return ret;
  140. }
  141. static int da9052_gpio_direction_output(struct gpio_chip *gc,
  142. unsigned offset, int value)
  143. {
  144. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  145. unsigned char register_value;
  146. int ret;
  147. /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
  148. register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
  149. value << 3;
  150. if (da9052_gpio_port_odd(offset))
  151. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  152. DA9052_GPIO_0_1_REG,
  153. DA9052_GPIO_MASK_UPPER_NIBBLE,
  154. (register_value <<
  155. DA9052_GPIO_NIBBLE_SHIFT));
  156. else
  157. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  158. DA9052_GPIO_0_1_REG,
  159. DA9052_GPIO_MASK_LOWER_NIBBLE,
  160. register_value);
  161. return ret;
  162. }
  163. static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
  164. {
  165. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  166. struct da9052 *da9052 = gpio->da9052;
  167. return da9052->irq_base + DA9052_IRQ_GPI0 + offset;
  168. }
  169. static struct gpio_chip reference_gp __devinitdata = {
  170. .label = "da9052-gpio",
  171. .owner = THIS_MODULE,
  172. .get = da9052_gpio_get,
  173. .set = da9052_gpio_set,
  174. .direction_input = da9052_gpio_direction_input,
  175. .direction_output = da9052_gpio_direction_output,
  176. .to_irq = da9052_gpio_to_irq,
  177. .can_sleep = 1;
  178. .ngpio = 16;
  179. .base = -1;
  180. };
  181. static int __devinit da9052_gpio_probe(struct platform_device *pdev)
  182. {
  183. struct da9052_gpio *gpio;
  184. struct da9052_pdata *pdata;
  185. int ret;
  186. gpio = kzalloc(sizeof(*gpio), GFP_KERNEL);
  187. if (gpio == NULL)
  188. return -ENOMEM;
  189. gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
  190. pdata = gpio->da9052->dev->platform_data;
  191. gpio->gp = reference_gp;
  192. if (pdata && pdata->gpio_base)
  193. gpio->gp.base = pdata->gpio_base;
  194. ret = gpiochip_add(&gpio->gp);
  195. if (ret < 0) {
  196. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  197. goto err_mem;
  198. }
  199. platform_set_drvdata(pdev, gpio);
  200. return 0;
  201. err_mem:
  202. kfree(gpio);
  203. return ret;
  204. }
  205. static int __devexit da9052_gpio_remove(struct platform_device *pdev)
  206. {
  207. struct da9052_gpio *gpio = platform_get_drvdata(pdev);
  208. int ret;
  209. ret = gpiochip_remove(&gpio->gp);
  210. if (ret == 0)
  211. kfree(gpio);
  212. return ret;
  213. }
  214. static struct platform_driver da9052_gpio_driver = {
  215. .probe = da9052_gpio_probe,
  216. .remove = __devexit_p(da9052_gpio_remove),
  217. .driver = {
  218. .name = "da9052-gpio",
  219. .owner = THIS_MODULE,
  220. },
  221. };
  222. static int __init da9052_gpio_init(void)
  223. {
  224. return platform_driver_register(&da9052_gpio_driver);
  225. }
  226. module_init(da9052_gpio_init);
  227. static void __exit da9052_gpio_exit(void)
  228. {
  229. return platform_driver_unregister(&da9052_gpio_driver);
  230. }
  231. module_exit(da9052_gpio_exit);
  232. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  233. MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
  234. MODULE_LICENSE("GPL");
  235. MODULE_ALIAS("platform:da9052-gpio");