pm8xxx-gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/gpio.h>
  16. #include <linux/mfd/pm8xxx/core.h>
  17. #include <linux/mfd/pm8xxx/gpio.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. /* GPIO registers */
  25. #define SSBI_REG_ADDR_GPIO_BASE 0x150
  26. #define SSBI_REG_ADDR_GPIO(n) (SSBI_REG_ADDR_GPIO_BASE + n)
  27. /* GPIO */
  28. #define PM_GPIO_BANK_MASK 0x70
  29. #define PM_GPIO_BANK_SHIFT 4
  30. #define PM_GPIO_WRITE 0x80
  31. /* Bank 0 */
  32. #define PM_GPIO_VIN_MASK 0x0E
  33. #define PM_GPIO_VIN_SHIFT 1
  34. #define PM_GPIO_MODE_ENABLE 0x01
  35. /* Bank 1 */
  36. #define PM_GPIO_MODE_MASK 0x0C
  37. #define PM_GPIO_MODE_SHIFT 2
  38. #define PM_GPIO_OUT_BUFFER 0x02
  39. #define PM_GPIO_OUT_INVERT 0x01
  40. #define PM_GPIO_MODE_OFF 3
  41. #define PM_GPIO_MODE_OUTPUT 2
  42. #define PM_GPIO_MODE_INPUT 0
  43. #define PM_GPIO_MODE_BOTH 1
  44. /* Bank 2 */
  45. #define PM_GPIO_PULL_MASK 0x0E
  46. #define PM_GPIO_PULL_SHIFT 1
  47. /* Bank 3 */
  48. #define PM_GPIO_OUT_STRENGTH_MASK 0x0C
  49. #define PM_GPIO_OUT_STRENGTH_SHIFT 2
  50. #define PM_GPIO_PIN_ENABLE 0x00
  51. #define PM_GPIO_PIN_DISABLE 0x01
  52. /* Bank 4 */
  53. #define PM_GPIO_FUNC_MASK 0x0E
  54. #define PM_GPIO_FUNC_SHIFT 1
  55. /* Bank 5 */
  56. #define PM_GPIO_NON_INT_POL_INV 0x08
  57. #define PM_GPIO_BANKS 6
  58. struct pm_gpio_chip {
  59. struct list_head link;
  60. struct gpio_chip gpio_chip;
  61. spinlock_t pm_lock;
  62. u8 *bank1;
  63. int irq_base;
  64. };
  65. static LIST_HEAD(pm_gpio_chips);
  66. static DEFINE_MUTEX(pm_gpio_chips_lock);
  67. static int pm_gpio_get(struct pm_gpio_chip *pm_gpio_chip, unsigned gpio)
  68. {
  69. int mode;
  70. /* Get gpio value from config bank 1 if output gpio.
  71. Get gpio value from IRQ RT status register for all other gpio modes.
  72. */
  73. mode = (pm_gpio_chip->bank1[gpio] & PM_GPIO_MODE_MASK) >>
  74. PM_GPIO_MODE_SHIFT;
  75. if (mode == PM_GPIO_MODE_OUTPUT)
  76. return pm_gpio_chip->bank1[gpio] & PM_GPIO_OUT_INVERT;
  77. else
  78. return pm8xxx_read_irq_stat(pm_gpio_chip->gpio_chip.dev->parent,
  79. pm_gpio_chip->irq_base + gpio);
  80. }
  81. static int pm_gpio_set(struct pm_gpio_chip *pm_gpio_chip,
  82. unsigned gpio, int value)
  83. {
  84. int rc;
  85. u8 bank1;
  86. unsigned long flags;
  87. spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
  88. bank1 = PM_GPIO_WRITE
  89. | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_OUT_INVERT);
  90. if (value)
  91. bank1 |= PM_GPIO_OUT_INVERT;
  92. pm_gpio_chip->bank1[gpio] = bank1;
  93. rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
  94. SSBI_REG_ADDR_GPIO(gpio), bank1);
  95. spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
  96. if (rc)
  97. pr_err("FAIL pm8xxx_writeb(): rc=%d. "
  98. "(gpio=%d, value=%d)\n",
  99. rc, gpio, value);
  100. return rc;
  101. }
  102. static int dir_map[] = {
  103. PM_GPIO_MODE_OFF,
  104. PM_GPIO_MODE_OUTPUT,
  105. PM_GPIO_MODE_INPUT,
  106. PM_GPIO_MODE_BOTH,
  107. };
  108. static int pm_gpio_set_direction(struct pm_gpio_chip *pm_gpio_chip,
  109. unsigned gpio, int direction)
  110. {
  111. int rc;
  112. u8 bank1;
  113. unsigned long flags;
  114. if (!direction || pm_gpio_chip == NULL)
  115. return -EINVAL;
  116. spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
  117. bank1 = PM_GPIO_WRITE
  118. | (pm_gpio_chip->bank1[gpio] & ~PM_GPIO_MODE_MASK);
  119. bank1 |= ((dir_map[direction] << PM_GPIO_MODE_SHIFT)
  120. & PM_GPIO_MODE_MASK);
  121. pm_gpio_chip->bank1[gpio] = bank1;
  122. rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
  123. SSBI_REG_ADDR_GPIO(gpio), bank1);
  124. spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
  125. if (rc)
  126. pr_err("Failed on pm8xxx_writeb(): rc=%d (GPIO config)\n",
  127. rc);
  128. return rc;
  129. }
  130. static int pm_gpio_init_bank1(struct pm_gpio_chip *pm_gpio_chip)
  131. {
  132. int i, rc;
  133. u8 bank;
  134. for (i = 0; i < pm_gpio_chip->gpio_chip.ngpio; i++) {
  135. bank = 1 << PM_GPIO_BANK_SHIFT;
  136. rc = pm8xxx_writeb(pm_gpio_chip->gpio_chip.dev->parent,
  137. SSBI_REG_ADDR_GPIO(i),
  138. bank);
  139. if (rc) {
  140. pr_err("error setting bank rc=%d\n", rc);
  141. return rc;
  142. }
  143. rc = pm8xxx_readb(pm_gpio_chip->gpio_chip.dev->parent,
  144. SSBI_REG_ADDR_GPIO(i),
  145. &pm_gpio_chip->bank1[i]);
  146. if (rc) {
  147. pr_err("error reading bank 1 rc=%d\n", rc);
  148. return rc;
  149. }
  150. }
  151. return 0;
  152. }
  153. static int pm_gpio_to_irq(struct gpio_chip *gpio_chip, unsigned offset)
  154. {
  155. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  156. return pm_gpio_chip->irq_base + offset;
  157. }
  158. static int pm_gpio_read(struct gpio_chip *gpio_chip, unsigned offset)
  159. {
  160. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  161. return pm_gpio_get(pm_gpio_chip, offset);
  162. }
  163. static void pm_gpio_write(struct gpio_chip *gpio_chip,
  164. unsigned offset, int val)
  165. {
  166. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  167. pm_gpio_set(pm_gpio_chip, offset, val);
  168. }
  169. static int pm_gpio_direction_input(struct gpio_chip *gpio_chip,
  170. unsigned offset)
  171. {
  172. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  173. return pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_IN);
  174. }
  175. static int pm_gpio_direction_output(struct gpio_chip *gpio_chip,
  176. unsigned offset,
  177. int val)
  178. {
  179. int ret;
  180. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  181. ret = pm_gpio_set_direction(pm_gpio_chip, offset, PM_GPIO_DIR_OUT);
  182. if (!ret)
  183. ret = pm_gpio_set(pm_gpio_chip, offset, val);
  184. return ret;
  185. }
  186. static void pm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *gpio_chip)
  187. {
  188. static const char * const cmode[] = { "in", "in/out", "out", "off" };
  189. struct pm_gpio_chip *pm_gpio_chip = dev_get_drvdata(gpio_chip->dev);
  190. u8 mode, state, bank;
  191. const char *label;
  192. int i, j;
  193. for (i = 0; i < gpio_chip->ngpio; i++) {
  194. label = gpiochip_is_requested(gpio_chip, i);
  195. mode = (pm_gpio_chip->bank1[i] & PM_GPIO_MODE_MASK) >>
  196. PM_GPIO_MODE_SHIFT;
  197. state = pm_gpio_get(pm_gpio_chip, i);
  198. seq_printf(s, "gpio-%-3d (%-12.12s) %-10.10s"
  199. " %s",
  200. gpio_chip->base + i,
  201. label ? label : "--",
  202. cmode[mode],
  203. state ? "hi" : "lo");
  204. for (j = 0; j < PM_GPIO_BANKS; j++) {
  205. bank = j << PM_GPIO_BANK_SHIFT;
  206. pm8xxx_writeb(gpio_chip->dev->parent,
  207. SSBI_REG_ADDR_GPIO(i),
  208. bank);
  209. pm8xxx_readb(gpio_chip->dev->parent,
  210. SSBI_REG_ADDR_GPIO(i),
  211. &bank);
  212. seq_printf(s, " 0x%02x", bank);
  213. }
  214. seq_printf(s, "\n");
  215. }
  216. }
  217. static int __devinit pm_gpio_probe(struct platform_device *pdev)
  218. {
  219. int ret;
  220. const struct pm8xxx_gpio_platform_data *pdata = pdev->dev.platform_data;
  221. struct pm_gpio_chip *pm_gpio_chip;
  222. if (!pdata) {
  223. pr_err("missing platform data\n");
  224. return -EINVAL;
  225. }
  226. pm_gpio_chip = kzalloc(sizeof(struct pm_gpio_chip), GFP_KERNEL);
  227. if (!pm_gpio_chip) {
  228. pr_err("Cannot allocate pm_gpio_chip\n");
  229. return -ENOMEM;
  230. }
  231. pm_gpio_chip->bank1 = kzalloc(sizeof(u8) * pdata->gpio_cdata.ngpios,
  232. GFP_KERNEL);
  233. if (!pm_gpio_chip->bank1) {
  234. pr_err("Cannot allocate pm_gpio_chip->bank1\n");
  235. ret = -ENOMEM;
  236. goto free_chip;
  237. }
  238. spin_lock_init(&pm_gpio_chip->pm_lock);
  239. pm_gpio_chip->gpio_chip.label = "pm-gpio";
  240. pm_gpio_chip->gpio_chip.direction_input = pm_gpio_direction_input;
  241. pm_gpio_chip->gpio_chip.direction_output = pm_gpio_direction_output;
  242. pm_gpio_chip->gpio_chip.to_irq = pm_gpio_to_irq;
  243. pm_gpio_chip->gpio_chip.get = pm_gpio_read;
  244. pm_gpio_chip->gpio_chip.set = pm_gpio_write;
  245. pm_gpio_chip->gpio_chip.dbg_show = pm_gpio_dbg_show;
  246. pm_gpio_chip->gpio_chip.ngpio = pdata->gpio_cdata.ngpios;
  247. pm_gpio_chip->gpio_chip.can_sleep = 0;
  248. pm_gpio_chip->gpio_chip.dev = &pdev->dev;
  249. pm_gpio_chip->gpio_chip.base = pdata->gpio_base;
  250. pm_gpio_chip->irq_base = platform_get_irq(pdev, 0);
  251. mutex_lock(&pm_gpio_chips_lock);
  252. list_add(&pm_gpio_chip->link, &pm_gpio_chips);
  253. mutex_unlock(&pm_gpio_chips_lock);
  254. platform_set_drvdata(pdev, pm_gpio_chip);
  255. ret = gpiochip_add(&pm_gpio_chip->gpio_chip);
  256. if (ret) {
  257. pr_err("gpiochip_add failed ret = %d\n", ret);
  258. goto reset_drvdata;
  259. }
  260. ret = pm_gpio_init_bank1(pm_gpio_chip);
  261. if (ret) {
  262. pr_err("gpio init bank failed ret = %d\n", ret);
  263. goto remove_chip;
  264. }
  265. pr_info("OK: base=%d, ngpio=%d\n", pm_gpio_chip->gpio_chip.base,
  266. pm_gpio_chip->gpio_chip.ngpio);
  267. return 0;
  268. remove_chip:
  269. if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
  270. pr_err("failed to remove gpio chip\n");
  271. reset_drvdata:
  272. platform_set_drvdata(pdev, NULL);
  273. kfree(pm_gpio_chip->bank1);
  274. free_chip:
  275. kfree(pm_gpio_chip);
  276. return ret;
  277. }
  278. static int __devexit pm_gpio_remove(struct platform_device *pdev)
  279. {
  280. struct pm_gpio_chip *pm_gpio_chip
  281. = platform_get_drvdata(pdev);
  282. mutex_lock(&pm_gpio_chips_lock);
  283. list_del(&pm_gpio_chip->link);
  284. mutex_unlock(&pm_gpio_chips_lock);
  285. platform_set_drvdata(pdev, NULL);
  286. if (gpiochip_remove(&pm_gpio_chip->gpio_chip))
  287. pr_err("failed to remove gpio chip\n");
  288. kfree(pm_gpio_chip->bank1);
  289. kfree(pm_gpio_chip);
  290. return 0;
  291. }
  292. int pm8xxx_gpio_config(int gpio, struct pm_gpio *param)
  293. {
  294. int rc, pm_gpio = -EINVAL;
  295. u8 bank[8];
  296. unsigned long flags;
  297. struct pm_gpio_chip *pm_gpio_chip;
  298. struct gpio_chip *gpio_chip;
  299. if (param == NULL)
  300. return -EINVAL;
  301. mutex_lock(&pm_gpio_chips_lock);
  302. list_for_each_entry(pm_gpio_chip, &pm_gpio_chips, link) {
  303. gpio_chip = &pm_gpio_chip->gpio_chip;
  304. if (gpio >= gpio_chip->base
  305. && gpio < gpio_chip->base + gpio_chip->ngpio) {
  306. pm_gpio = gpio - gpio_chip->base;
  307. break;
  308. }
  309. }
  310. mutex_unlock(&pm_gpio_chips_lock);
  311. if (pm_gpio < 0) {
  312. pr_err("called on gpio %d not handled by any pmic\n", gpio);
  313. return -EINVAL;
  314. }
  315. /* Select banks and configure the gpio */
  316. bank[0] = PM_GPIO_WRITE |
  317. ((param->vin_sel << PM_GPIO_VIN_SHIFT) &
  318. PM_GPIO_VIN_MASK) |
  319. PM_GPIO_MODE_ENABLE;
  320. bank[1] = PM_GPIO_WRITE |
  321. ((1 << PM_GPIO_BANK_SHIFT) &
  322. PM_GPIO_BANK_MASK) |
  323. ((dir_map[param->direction] <<
  324. PM_GPIO_MODE_SHIFT) &
  325. PM_GPIO_MODE_MASK) |
  326. ((param->direction & PM_GPIO_DIR_OUT) ?
  327. ((param->output_buffer & 1) ?
  328. PM_GPIO_OUT_BUFFER : 0) : 0) |
  329. ((param->direction & PM_GPIO_DIR_OUT) ?
  330. param->output_value & 0x01 : 0);
  331. bank[2] = PM_GPIO_WRITE |
  332. ((2 << PM_GPIO_BANK_SHIFT) &
  333. PM_GPIO_BANK_MASK) |
  334. ((param->pull << PM_GPIO_PULL_SHIFT) &
  335. PM_GPIO_PULL_MASK);
  336. bank[3] = PM_GPIO_WRITE |
  337. ((3 << PM_GPIO_BANK_SHIFT) &
  338. PM_GPIO_BANK_MASK) |
  339. ((param->out_strength <<
  340. PM_GPIO_OUT_STRENGTH_SHIFT) &
  341. PM_GPIO_OUT_STRENGTH_MASK) |
  342. (param->disable_pin ?
  343. PM_GPIO_PIN_DISABLE : PM_GPIO_PIN_ENABLE);
  344. bank[4] = PM_GPIO_WRITE |
  345. ((4 << PM_GPIO_BANK_SHIFT) &
  346. PM_GPIO_BANK_MASK) |
  347. ((param->function << PM_GPIO_FUNC_SHIFT) &
  348. PM_GPIO_FUNC_MASK);
  349. bank[5] = PM_GPIO_WRITE |
  350. ((5 << PM_GPIO_BANK_SHIFT) & PM_GPIO_BANK_MASK) |
  351. (param->inv_int_pol ? 0 : PM_GPIO_NON_INT_POL_INV);
  352. spin_lock_irqsave(&pm_gpio_chip->pm_lock, flags);
  353. /* Remember bank1 for later use */
  354. pm_gpio_chip->bank1[pm_gpio] = bank[1];
  355. rc = pm8xxx_write_buf(pm_gpio_chip->gpio_chip.dev->parent,
  356. SSBI_REG_ADDR_GPIO(pm_gpio), bank, 6);
  357. spin_unlock_irqrestore(&pm_gpio_chip->pm_lock, flags);
  358. if (rc)
  359. pr_err("Failed on pm8xxx_write_buf() rc=%d (GPIO config)\n",
  360. rc);
  361. return rc;
  362. }
  363. EXPORT_SYMBOL(pm8xxx_gpio_config);
  364. static struct platform_driver pm_gpio_driver = {
  365. .probe = pm_gpio_probe,
  366. .remove = __devexit_p(pm_gpio_remove),
  367. .driver = {
  368. .name = PM8XXX_GPIO_DEV_NAME,
  369. .owner = THIS_MODULE,
  370. },
  371. };
  372. static int __init pm_gpio_init(void)
  373. {
  374. return platform_driver_register(&pm_gpio_driver);
  375. }
  376. postcore_initcall(pm_gpio_init);
  377. static void __exit pm_gpio_exit(void)
  378. {
  379. platform_driver_unregister(&pm_gpio_driver);
  380. }
  381. module_exit(pm_gpio_exit);
  382. MODULE_LICENSE("GPL v2");
  383. MODULE_DESCRIPTION("PMIC GPIO driver");
  384. MODULE_VERSION("1.0");
  385. MODULE_ALIAS("platform:" PM8XXX_GPIO_DEV_NAME);