lcm_gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #if defined(MTK_LCM_DEVICE_TREE_SUPPORT)
  2. #include <linux/string.h>
  3. #include <linux/wait.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/gpio.h>
  6. #include <linux/pinctrl/consumer.h>
  7. #include <linux/of.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_gpio.h>
  11. #include <linux/of_irq.h>
  12. #include <asm-generic/gpio.h>
  13. #ifdef BUILD_LK
  14. #include <platform/upmu_common.h>
  15. #include <platform/upmu_hw.h>
  16. #include <platform/mt_gpio.h>
  17. #include <platform/mt_i2c.h>
  18. #include <platform/mt_pmic.h>
  19. #include <string.h>
  20. #else
  21. #ifdef CONFIG_MTK_LEGACY
  22. #include <mach/mt_pm_ldo.h> /* hwPowerOn */
  23. #include <mt-plat/upmu_common.h>
  24. #include <mach/upmu_sw.h>
  25. #include <mach/upmu_hw.h>
  26. #else
  27. #include <mt-plat/upmu_common.h>
  28. #include <mach/upmu_sw.h>
  29. #include <mach/upmu_hw.h>
  30. #endif
  31. #endif
  32. #ifdef CONFIG_MTK_LEGACY
  33. #include <mach/mt_gpio.h>
  34. #include <cust_gpio_usage.h>
  35. #include <cust_i2c.h>
  36. #else
  37. #include <mt-plat/mt_gpio.h>
  38. #endif
  39. #include "lcm_define.h"
  40. #include "lcm_drv.h"
  41. #include "lcm_gpio.h"
  42. #ifdef CONFIG_MTK_LEGACY
  43. #if defined(GPIO_LCD_BIAS_ENP_PIN)
  44. #define GPIO_65132_EN GPIO_LCD_BIAS_ENP_PIN
  45. #else
  46. #define GPIO_65132_EN 0
  47. #endif
  48. #else
  49. static struct pinctrl *_lcm_gpio;
  50. static struct pinctrl_state *_lcm_gpio_mode_default;
  51. static struct pinctrl_state *_lcm_gpio_mode[MAX_LCM_GPIO_MODE];
  52. static unsigned char _lcm_gpio_mode_list[MAX_LCM_GPIO_MODE][128] = {
  53. "lcm_mode_00",
  54. "lcm_mode_01",
  55. "lcm_mode_02",
  56. "lcm_mode_03",
  57. "lcm_mode_04",
  58. "lcm_mode_05",
  59. "lcm_mode_06",
  60. "lcm_mode_07"
  61. };
  62. static unsigned int GPIO_LCD_PWR_EN;
  63. static unsigned int GPIO_LCD_BL_EN;
  64. /* function definitions */
  65. static int __init _lcm_gpio_init(void);
  66. static void __exit _lcm_gpio_exit(void);
  67. static int _lcm_gpio_probe(struct device *dev);
  68. static int _lcm_gpio_remove(struct device *dev);
  69. static const struct of_device_id _lcm_gpio_of_ids[] = {
  70. {.compatible = "mediatek,lcm_mode",},
  71. {}
  72. };
  73. static struct platform_driver _lcm_gpio_driver = {
  74. .driver = {
  75. .name = LCM_GPIO_DEVICE,
  76. .probe = _lcm_gpio_probe,
  77. .remove = _lcm_gpio_remove,
  78. .of_match_table = _lcm_gpio_of_ids,
  79. },
  80. };
  81. #endif
  82. #ifdef CONFIG_MTK_LEGACY
  83. #else
  84. /* LCM GPIO probe */
  85. static int _lcm_gpio_probe(struct device *dev)
  86. {
  87. int ret;
  88. unsigned int mode;
  89. const struct of_device_id *match;
  90. pr_debug("[LCM][GPIO] enter %s, %d\n", __func__, __LINE__);
  91. _lcm_gpio = devm_pinctrl_get(dev);
  92. if (IS_ERR(_lcm_gpio)) {
  93. ret = PTR_ERR(_lcm_gpio);
  94. dev_err(dev, "[LCM][ERROR] Cannot find _lcm_gpio!\n");
  95. return ret;
  96. }
  97. _lcm_gpio_mode_default = pinctrl_lookup_state(_lcm_gpio, "default");
  98. if (IS_ERR(_lcm_gpio_mode_default)) {
  99. ret = PTR_ERR(_lcm_gpio_mode_default);
  100. dev_err(dev, "[LCM][ERROR] Cannot find lcm_mode_default %d!\n", ret);
  101. }
  102. for (mode = LCM_GPIO_MODE_00; mode < MAX_LCM_GPIO_MODE; mode++) {
  103. _lcm_gpio_mode[mode] = pinctrl_lookup_state(_lcm_gpio, _lcm_gpio_mode_list[mode]);
  104. if (IS_ERR(_lcm_gpio_mode[mode])) {
  105. ret = PTR_ERR(_lcm_gpio_mode[mode]);
  106. dev_err(dev, "[LCM][ERROR] Cannot find lcm_mode:%d!\n", mode);
  107. return ret;
  108. }
  109. }
  110. if (dev->of_node) {
  111. match = of_match_device(of_match_ptr(_lcm_gpio_of_ids), dev);
  112. if (!match) {
  113. pr_err("[LCM][ERROR] No device match found\n");
  114. return -ENODEV;
  115. }
  116. }
  117. GPIO_LCD_PWR_EN = of_get_named_gpio(dev->of_node, "lcm_power_gpio", 0);
  118. GPIO_LCD_BL_EN = of_get_named_gpio(dev->of_node, "lcm_bl_gpio", 0);
  119. ret = gpio_request(GPIO_LCD_PWR_EN, "lcm_power_gpio");
  120. if (ret < 0)
  121. pr_err("[LCM][ERROR] Unable to request GPIO_LCD_PWR_EN\n");
  122. ret = gpio_request(GPIO_LCD_BL_EN, "lcm_bl_gpio");
  123. if (ret < 0)
  124. pr_err("[LCM][ERROR] Unable to request GPIO_LCD_BL_EN\n");
  125. pr_debug("[LCM][GPIO] _lcm_gpio_get_info end!\n");
  126. return 0;
  127. }
  128. static int _lcm_gpio_remove(struct device *dev)
  129. {
  130. gpio_free(GPIO_LCD_BL_EN);
  131. gpio_free(GPIO_LCD_PWR_EN);
  132. return 0;
  133. }
  134. /* called when loaded into kernel */
  135. static int __init _lcm_gpio_init(void)
  136. {
  137. pr_debug("MediaTek LCM GPIO driver init\n");
  138. if (platform_driver_register(&_lcm_gpio_driver) != 0) {
  139. pr_err("unable to register LCM GPIO driver.\n");
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. /* should never be called */
  145. static void __exit _lcm_gpio_exit(void)
  146. {
  147. pr_debug("MediaTek LCM GPIO driver exit\n");
  148. platform_driver_unregister(&_lcm_gpio_driver);
  149. }
  150. #endif
  151. static LCM_STATUS _lcm_gpio_check_data(char type, const LCM_DATA_T1 *t1)
  152. {
  153. switch (type) {
  154. case LCM_GPIO_MODE:
  155. switch (t1->data) {
  156. case LCM_GPIO_MODE_00:
  157. case LCM_GPIO_MODE_01:
  158. case LCM_GPIO_MODE_02:
  159. case LCM_GPIO_MODE_03:
  160. case LCM_GPIO_MODE_04:
  161. case LCM_GPIO_MODE_05:
  162. case LCM_GPIO_MODE_06:
  163. case LCM_GPIO_MODE_07:
  164. break;
  165. default:
  166. pr_err("[LCM][ERROR] %s/%d: %d, %d\n", __func__, __LINE__, type, t1->data);
  167. return LCM_STATUS_ERROR;
  168. }
  169. break;
  170. case LCM_GPIO_DIR:
  171. switch (t1->data) {
  172. case LCM_GPIO_DIR_IN:
  173. case LCM_GPIO_DIR_OUT:
  174. break;
  175. default:
  176. pr_err("[LCM][ERROR] %s/%d: %d, %d\n", __func__, __LINE__, type, t1->data);
  177. return LCM_STATUS_ERROR;
  178. }
  179. break;
  180. case LCM_GPIO_OUT:
  181. switch (t1->data) {
  182. case LCM_GPIO_OUT_ZERO:
  183. case LCM_GPIO_OUT_ONE:
  184. break;
  185. default:
  186. pr_err("[LCM][ERROR] %s/%d: %d, %d\n", __func__, __LINE__, type, t1->data);
  187. return LCM_STATUS_ERROR;
  188. }
  189. break;
  190. default:
  191. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, type);
  192. return LCM_STATUS_ERROR;
  193. }
  194. return LCM_STATUS_OK;
  195. }
  196. LCM_STATUS lcm_gpio_set_data(char type, const LCM_DATA_T1 *t1)
  197. {
  198. /* check parameter is valid */
  199. if (LCM_STATUS_OK == _lcm_gpio_check_data(type, t1)) {
  200. switch (type) {
  201. #ifdef CONFIG_MTK_LEGACY
  202. case LCM_GPIO_MODE:
  203. mt_set_gpio_mode(GPIO_65132_EN, (unsigned int)t1->data);
  204. break;
  205. case LCM_GPIO_DIR:
  206. mt_set_gpio_dir(GPIO_65132_EN, (unsigned int)t1->data);
  207. break;
  208. case LCM_GPIO_OUT:
  209. mt_set_gpio_out(GPIO_65132_EN, (unsigned int)t1->data);
  210. break;
  211. #else
  212. case LCM_GPIO_MODE:
  213. pr_debug("[LCM][GPIO] %s/%d: set mode: %d\n", __func__, __LINE__,
  214. (unsigned int)t1->data);
  215. pinctrl_select_state(_lcm_gpio, _lcm_gpio_mode[(unsigned int)t1->data]);
  216. break;
  217. case LCM_GPIO_DIR:
  218. pr_debug("[LCM][GPIO] %s/%d: set dir: %d, %d\n", __func__, __LINE__,
  219. GPIO_LCD_PWR_EN, (unsigned int)t1->data);
  220. gpio_direction_output(GPIO_LCD_PWR_EN, (int)t1->data);
  221. break;
  222. case LCM_GPIO_OUT:
  223. pr_debug("[LCM][GPIO] %s/%d: set out: %d, %d\n", __func__, __LINE__,
  224. GPIO_LCD_PWR_EN, (unsigned int)t1->data);
  225. gpio_set_value(GPIO_LCD_PWR_EN, (int)t1->data);
  226. break;
  227. #endif
  228. default:
  229. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, type);
  230. return LCM_STATUS_ERROR;
  231. }
  232. } else {
  233. pr_err("[LCM][ERROR] %s: 0x%x, 0x%x\n", __func__, type, t1->data);
  234. return LCM_STATUS_ERROR;
  235. }
  236. return LCM_STATUS_OK;
  237. }
  238. #ifdef CONFIG_MTK_LEGACY
  239. #else
  240. module_init(_lcm_gpio_init);
  241. module_exit(_lcm_gpio_exit);
  242. MODULE_LICENSE("GPL");
  243. MODULE_DESCRIPTION("MediaTek LCM GPIO driver");
  244. MODULE_AUTHOR("Joey Pan<joey.pan@mediatek.com>");
  245. #endif
  246. #endif