gpio-da9052.c 6.0 KB

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