gpio-da9052.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #define DA9052_INPUT 1
  25. #define DA9052_OUTPUT_OPENDRAIN 2
  26. #define DA9052_OUTPUT_PUSHPULL 3
  27. #define DA9052_SUPPLY_VDD_IO1 0
  28. #define DA9052_DEBOUNCING_OFF 0
  29. #define DA9052_DEBOUNCING_ON 1
  30. #define DA9052_OUTPUT_LOWLEVEL 0
  31. #define DA9052_ACTIVE_LOW 0
  32. #define DA9052_ACTIVE_HIGH 1
  33. #define DA9052_GPIO_MAX_PORTS_PER_REGISTER 8
  34. #define DA9052_GPIO_SHIFT_COUNT(no) (no%8)
  35. #define DA9052_GPIO_MASK_UPPER_NIBBLE 0xF0
  36. #define DA9052_GPIO_MASK_LOWER_NIBBLE 0x0F
  37. #define DA9052_GPIO_NIBBLE_SHIFT 4
  38. #define DA9052_IRQ_GPI0 16
  39. #define DA9052_GPIO_ODD_SHIFT 7
  40. #define DA9052_GPIO_EVEN_SHIFT 3
  41. struct da9052_gpio {
  42. struct da9052 *da9052;
  43. struct gpio_chip gp;
  44. };
  45. static inline struct da9052_gpio *to_da9052_gpio(struct gpio_chip *chip)
  46. {
  47. return container_of(chip, struct da9052_gpio, gp);
  48. }
  49. static unsigned char da9052_gpio_port_odd(unsigned offset)
  50. {
  51. return offset % 2;
  52. }
  53. static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
  54. {
  55. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  56. int da9052_port_direction = 0;
  57. int ret;
  58. ret = da9052_reg_read(gpio->da9052,
  59. DA9052_GPIO_0_1_REG + (offset >> 1));
  60. if (ret < 0)
  61. return ret;
  62. if (da9052_gpio_port_odd(offset)) {
  63. da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
  64. da9052_port_direction >>= 4;
  65. } else {
  66. da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
  67. }
  68. switch (da9052_port_direction) {
  69. case DA9052_INPUT:
  70. if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
  71. ret = da9052_reg_read(gpio->da9052,
  72. DA9052_STATUS_C_REG);
  73. else
  74. ret = da9052_reg_read(gpio->da9052,
  75. DA9052_STATUS_D_REG);
  76. if (ret < 0)
  77. return ret;
  78. if (ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)))
  79. return 1;
  80. else
  81. return 0;
  82. case DA9052_OUTPUT_PUSHPULL:
  83. if (da9052_gpio_port_odd(offset))
  84. return ret & DA9052_GPIO_ODD_PORT_MODE;
  85. else
  86. return ret & DA9052_GPIO_EVEN_PORT_MODE;
  87. default:
  88. return -EINVAL;
  89. }
  90. }
  91. static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  92. {
  93. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  94. int ret;
  95. if (da9052_gpio_port_odd(offset)) {
  96. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  97. DA9052_GPIO_0_1_REG,
  98. DA9052_GPIO_ODD_PORT_MODE,
  99. value << DA9052_GPIO_ODD_SHIFT);
  100. if (ret != 0)
  101. dev_err(gpio->da9052->dev,
  102. "Failed to updated gpio odd reg,%d",
  103. ret);
  104. } else {
  105. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  106. DA9052_GPIO_0_1_REG,
  107. DA9052_GPIO_EVEN_PORT_MODE,
  108. value << DA9052_GPIO_EVEN_SHIFT);
  109. if (ret != 0)
  110. dev_err(gpio->da9052->dev,
  111. "Failed to updated gpio even reg,%d",
  112. ret);
  113. }
  114. }
  115. static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  116. {
  117. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  118. unsigned char register_value;
  119. int ret;
  120. /* Format: function - 2 bits type - 1 bit mode - 1 bit */
  121. register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
  122. DA9052_DEBOUNCING_ON << 3;
  123. if (da9052_gpio_port_odd(offset))
  124. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  125. DA9052_GPIO_0_1_REG,
  126. DA9052_GPIO_MASK_UPPER_NIBBLE,
  127. (register_value <<
  128. DA9052_GPIO_NIBBLE_SHIFT));
  129. else
  130. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  131. DA9052_GPIO_0_1_REG,
  132. DA9052_GPIO_MASK_LOWER_NIBBLE,
  133. register_value);
  134. return ret;
  135. }
  136. static int da9052_gpio_direction_output(struct gpio_chip *gc,
  137. unsigned offset, int value)
  138. {
  139. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  140. unsigned char register_value;
  141. int ret;
  142. /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
  143. register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
  144. value << 3;
  145. if (da9052_gpio_port_odd(offset))
  146. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  147. DA9052_GPIO_0_1_REG,
  148. DA9052_GPIO_MASK_UPPER_NIBBLE,
  149. (register_value <<
  150. DA9052_GPIO_NIBBLE_SHIFT));
  151. else
  152. ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
  153. DA9052_GPIO_0_1_REG,
  154. DA9052_GPIO_MASK_LOWER_NIBBLE,
  155. register_value);
  156. return ret;
  157. }
  158. static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
  159. {
  160. struct da9052_gpio *gpio = to_da9052_gpio(gc);
  161. struct da9052 *da9052 = gpio->da9052;
  162. int irq;
  163. irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset);
  164. return irq;
  165. }
  166. static struct gpio_chip reference_gp = {
  167. .label = "da9052-gpio",
  168. .owner = THIS_MODULE,
  169. .get = da9052_gpio_get,
  170. .set = da9052_gpio_set,
  171. .direction_input = da9052_gpio_direction_input,
  172. .direction_output = da9052_gpio_direction_output,
  173. .to_irq = da9052_gpio_to_irq,
  174. .can_sleep = true,
  175. .ngpio = 16,
  176. .base = -1,
  177. };
  178. static int da9052_gpio_probe(struct platform_device *pdev)
  179. {
  180. struct da9052_gpio *gpio;
  181. struct da9052_pdata *pdata;
  182. int ret;
  183. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  184. if (!gpio)
  185. return -ENOMEM;
  186. gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
  187. pdata = dev_get_platdata(gpio->da9052->dev);
  188. gpio->gp = reference_gp;
  189. if (pdata && pdata->gpio_base)
  190. gpio->gp.base = pdata->gpio_base;
  191. ret = gpiochip_add(&gpio->gp);
  192. if (ret < 0) {
  193. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  194. return ret;
  195. }
  196. platform_set_drvdata(pdev, gpio);
  197. return 0;
  198. }
  199. static int da9052_gpio_remove(struct platform_device *pdev)
  200. {
  201. struct da9052_gpio *gpio = platform_get_drvdata(pdev);
  202. gpiochip_remove(&gpio->gp);
  203. return 0;
  204. }
  205. static struct platform_driver da9052_gpio_driver = {
  206. .probe = da9052_gpio_probe,
  207. .remove = da9052_gpio_remove,
  208. .driver = {
  209. .name = "da9052-gpio",
  210. },
  211. };
  212. module_platform_driver(da9052_gpio_driver);
  213. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  214. MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
  215. MODULE_LICENSE("GPL");
  216. MODULE_ALIAS("platform:da9052-gpio");