pm8xxx-mpp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Qualcomm PM8XXX Multi-Purpose Pin (MPP) driver
  3. *
  4. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/gpio.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/mfd/pm8xxx/core.h>
  23. #include <linux/mfd/pm8xxx/mpp.h>
  24. /* MPP Type */
  25. #define PM8XXX_MPP_TYPE_MASK 0xE0
  26. #define PM8XXX_MPP_TYPE_SHIFT 5
  27. /* MPP Config Level */
  28. #define PM8XXX_MPP_CONFIG_LVL_MASK 0x1C
  29. #define PM8XXX_MPP_CONFIG_LVL_SHIFT 2
  30. /* MPP Config Control */
  31. #define PM8XXX_MPP_CONFIG_CTRL_MASK 0x03
  32. #define PM8XXX_MPP_CONFIG_CTRL_SHIFT 0
  33. struct pm8xxx_mpp_chip {
  34. struct list_head link;
  35. struct gpio_chip gpio_chip;
  36. spinlock_t pm_lock;
  37. u8 *ctrl_reg;
  38. int mpp_base;
  39. int irq_base;
  40. int nmpps;
  41. u16 base_addr;
  42. };
  43. static LIST_HEAD(pm8xxx_mpp_chips);
  44. static DEFINE_MUTEX(pm8xxx_mpp_chips_lock);
  45. static int pm8xxx_mpp_write(struct pm8xxx_mpp_chip *mpp_chip, u16 offset,
  46. u8 val, u8 mask)
  47. {
  48. u8 reg;
  49. int rc;
  50. unsigned long flags;
  51. spin_lock_irqsave(&mpp_chip->pm_lock, flags);
  52. reg = (mpp_chip->ctrl_reg[offset] & ~mask) | (val & mask);
  53. rc = pm8xxx_writeb(mpp_chip->gpio_chip.dev->parent,
  54. mpp_chip->base_addr + offset, reg);
  55. if (!rc)
  56. mpp_chip->ctrl_reg[offset] = reg;
  57. spin_unlock_irqrestore(&mpp_chip->pm_lock, flags);
  58. return rc;
  59. }
  60. static int pm8xxx_mpp_to_irq(struct gpio_chip *chip, unsigned offset)
  61. {
  62. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  63. return mpp_chip->irq_base + offset;
  64. }
  65. static int pm8xxx_mpp_get(struct gpio_chip *chip, unsigned offset)
  66. {
  67. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  68. int rc;
  69. if ((mpp_chip->ctrl_reg[offset] & PM8XXX_MPP_TYPE_MASK) >>
  70. PM8XXX_MPP_TYPE_SHIFT == PM8XXX_MPP_TYPE_D_OUTPUT)
  71. rc = mpp_chip->ctrl_reg[offset] & PM8XXX_MPP_CONFIG_CTRL_MASK;
  72. else
  73. rc = pm8xxx_read_irq_stat(mpp_chip->gpio_chip.dev->parent,
  74. mpp_chip->irq_base + offset);
  75. return rc;
  76. }
  77. static void pm8xxx_mpp_set(struct gpio_chip *chip, unsigned offset, int val)
  78. {
  79. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  80. u8 reg = val ? PM8XXX_MPP_DOUT_CTRL_HIGH : PM8XXX_MPP_DOUT_CTRL_LOW;
  81. int rc;
  82. rc = pm8xxx_mpp_write(mpp_chip, offset, reg,
  83. PM8XXX_MPP_CONFIG_CTRL_MASK);
  84. if (rc)
  85. pr_err("pm8xxx_mpp_write(): rc=%d\n", rc);
  86. }
  87. static int pm8xxx_mpp_dir_input(struct gpio_chip *chip, unsigned offset)
  88. {
  89. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  90. int rc = pm8xxx_mpp_write(mpp_chip, offset,
  91. PM8XXX_MPP_TYPE_D_INPUT << PM8XXX_MPP_TYPE_SHIFT,
  92. PM8XXX_MPP_TYPE_MASK);
  93. if (rc)
  94. pr_err("pm8xxx_mpp_write(): rc=%d\n", rc);
  95. return rc;
  96. }
  97. static int pm8xxx_mpp_dir_output(struct gpio_chip *chip,
  98. unsigned offset, int val)
  99. {
  100. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  101. u8 reg = (PM8XXX_MPP_TYPE_D_OUTPUT << PM8XXX_MPP_TYPE_SHIFT) |
  102. (val & PM8XXX_MPP_CONFIG_CTRL_MASK);
  103. u8 mask = PM8XXX_MPP_TYPE_MASK | PM8XXX_MPP_CONFIG_CTRL_MASK;
  104. int rc = pm8xxx_mpp_write(mpp_chip, offset, reg, mask);
  105. if (rc)
  106. pr_err("pm8xxx_mpp_write(): rc=%d\n", rc);
  107. return rc;
  108. }
  109. static void pm8xxx_mpp_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  110. {
  111. static const char * const ctype[] = { "d_in", "d_out", "bi_dir",
  112. "a_in", "a_out", "sink",
  113. "dtest_sink", "dtest_out"
  114. };
  115. struct pm8xxx_mpp_chip *mpp_chip = dev_get_drvdata(chip->dev);
  116. u8 type, state;
  117. const char *label;
  118. int i;
  119. for (i = 0; i < mpp_chip->nmpps; i++) {
  120. label = gpiochip_is_requested(chip, i);
  121. type = (mpp_chip->ctrl_reg[i] & PM8XXX_MPP_TYPE_MASK) >>
  122. PM8XXX_MPP_TYPE_SHIFT;
  123. state = pm8xxx_mpp_get(chip, i);
  124. seq_printf(s, "gpio-%-3d (%-12.12s) %-10.10s"
  125. " %s 0x%02x\n",
  126. chip->base + i,
  127. label ? label : "--",
  128. ctype[type],
  129. state ? "hi" : "lo",
  130. mpp_chip->ctrl_reg[i]);
  131. }
  132. }
  133. int pm8xxx_mpp_config(unsigned mpp, struct pm8xxx_mpp_config_data *config)
  134. {
  135. struct pm8xxx_mpp_chip *mpp_chip;
  136. int rc, found = 0;
  137. u8 config_reg, mask;
  138. if (!config) {
  139. pr_err("config not specified for MPP %d\n", mpp);
  140. return -EINVAL;
  141. }
  142. mutex_lock(&pm8xxx_mpp_chips_lock);
  143. list_for_each_entry(mpp_chip, &pm8xxx_mpp_chips, link) {
  144. if (mpp >= mpp_chip->mpp_base
  145. && mpp < mpp_chip->mpp_base + mpp_chip->nmpps) {
  146. found = 1;
  147. break;
  148. }
  149. }
  150. mutex_unlock(&pm8xxx_mpp_chips_lock);
  151. if (!found) {
  152. pr_err("called on mpp %d not handled by any pmic\n", mpp);
  153. return -EINVAL;
  154. }
  155. mask = PM8XXX_MPP_TYPE_MASK | PM8XXX_MPP_CONFIG_LVL_MASK |
  156. PM8XXX_MPP_CONFIG_CTRL_MASK;
  157. config_reg = (config->type << PM8XXX_MPP_TYPE_SHIFT)
  158. & PM8XXX_MPP_TYPE_MASK;
  159. config_reg |= (config->level << PM8XXX_MPP_CONFIG_LVL_SHIFT)
  160. & PM8XXX_MPP_CONFIG_LVL_MASK;
  161. config_reg |= config->control & PM8XXX_MPP_CONFIG_CTRL_MASK;
  162. rc = pm8xxx_mpp_write(mpp_chip, mpp - mpp_chip->mpp_base, config_reg,
  163. mask);
  164. if (rc)
  165. pr_err("pm8xxx_mpp_write(): rc=%d\n", rc);
  166. return rc;
  167. }
  168. EXPORT_SYMBOL_GPL(pm8xxx_mpp_config);
  169. static int __devinit pm8xxx_mpp_reg_init(struct pm8xxx_mpp_chip *mpp_chip)
  170. {
  171. int rc, i;
  172. for (i = 0; i < mpp_chip->nmpps; i++) {
  173. rc = pm8xxx_readb(mpp_chip->gpio_chip.dev->parent,
  174. mpp_chip->base_addr + i,
  175. &mpp_chip->ctrl_reg[i]);
  176. if (rc) {
  177. pr_err("failed to read register 0x%x rc=%d\n",
  178. mpp_chip->base_addr + i, rc);
  179. return rc;
  180. }
  181. }
  182. return 0;
  183. }
  184. static int __devinit pm8xxx_mpp_probe(struct platform_device *pdev)
  185. {
  186. int rc;
  187. const struct pm8xxx_mpp_platform_data *pdata = pdev->dev.platform_data;
  188. struct pm8xxx_mpp_chip *mpp_chip;
  189. if (!pdata) {
  190. pr_err("missing platform data\n");
  191. return -EINVAL;
  192. }
  193. mpp_chip = kzalloc(sizeof(struct pm8xxx_mpp_chip), GFP_KERNEL);
  194. if (!mpp_chip) {
  195. pr_err("Cannot allocate %d bytes\n",
  196. sizeof(struct pm8xxx_mpp_chip));
  197. return -ENOMEM;
  198. }
  199. mpp_chip->ctrl_reg = kzalloc(pdata->core_data.nmpps, GFP_KERNEL);
  200. if (!mpp_chip->ctrl_reg) {
  201. pr_err("Cannot allocate %d bytes\n", pdata->core_data.nmpps);
  202. rc = -ENOMEM;
  203. goto free_mpp_chip;
  204. }
  205. spin_lock_init(&mpp_chip->pm_lock);
  206. mpp_chip->gpio_chip.label = PM8XXX_MPP_DEV_NAME;
  207. mpp_chip->gpio_chip.direction_input = pm8xxx_mpp_dir_input;
  208. mpp_chip->gpio_chip.direction_output = pm8xxx_mpp_dir_output;
  209. mpp_chip->gpio_chip.to_irq = pm8xxx_mpp_to_irq;
  210. mpp_chip->gpio_chip.get = pm8xxx_mpp_get;
  211. mpp_chip->gpio_chip.set = pm8xxx_mpp_set;
  212. mpp_chip->gpio_chip.dbg_show = pm8xxx_mpp_dbg_show;
  213. mpp_chip->gpio_chip.ngpio = pdata->core_data.nmpps;
  214. mpp_chip->gpio_chip.can_sleep = 0;
  215. mpp_chip->gpio_chip.dev = &pdev->dev;
  216. mpp_chip->gpio_chip.base = pdata->mpp_base;
  217. mpp_chip->irq_base = platform_get_irq(pdev, 0);
  218. mpp_chip->mpp_base = pdata->mpp_base;
  219. mpp_chip->base_addr = pdata->core_data.base_addr;
  220. mpp_chip->nmpps = pdata->core_data.nmpps;
  221. mutex_lock(&pm8xxx_mpp_chips_lock);
  222. list_add(&mpp_chip->link, &pm8xxx_mpp_chips);
  223. mutex_unlock(&pm8xxx_mpp_chips_lock);
  224. platform_set_drvdata(pdev, mpp_chip);
  225. rc = gpiochip_add(&mpp_chip->gpio_chip);
  226. if (rc) {
  227. pr_err("gpiochip_add failed, rc=%d\n", rc);
  228. goto reset_drvdata;
  229. }
  230. rc = pm8xxx_mpp_reg_init(mpp_chip);
  231. if (rc) {
  232. pr_err("failed to read MPP ctrl registers, rc=%d\n", rc);
  233. goto remove_chip;
  234. }
  235. pr_info("OK: base=%d, ngpio=%d\n", mpp_chip->gpio_chip.base,
  236. mpp_chip->gpio_chip.ngpio);
  237. return 0;
  238. remove_chip:
  239. if (gpiochip_remove(&mpp_chip->gpio_chip))
  240. pr_err("failed to remove gpio chip\n");
  241. reset_drvdata:
  242. platform_set_drvdata(pdev, NULL);
  243. free_mpp_chip:
  244. kfree(mpp_chip);
  245. return rc;
  246. }
  247. static int __devexit pm8xxx_mpp_remove(struct platform_device *pdev)
  248. {
  249. struct pm8xxx_mpp_chip *mpp_chip = platform_get_drvdata(pdev);
  250. mutex_lock(&pm8xxx_mpp_chips_lock);
  251. list_del(&mpp_chip->link);
  252. mutex_unlock(&pm8xxx_mpp_chips_lock);
  253. platform_set_drvdata(pdev, NULL);
  254. if (gpiochip_remove(&mpp_chip->gpio_chip))
  255. pr_err("failed to remove gpio chip\n");
  256. kfree(mpp_chip->ctrl_reg);
  257. kfree(mpp_chip);
  258. return 0;
  259. }
  260. static struct platform_driver pm8xxx_mpp_driver = {
  261. .probe = pm8xxx_mpp_probe,
  262. .remove = __devexit_p(pm8xxx_mpp_remove),
  263. .driver = {
  264. .name = PM8XXX_MPP_DEV_NAME,
  265. .owner = THIS_MODULE,
  266. },
  267. };
  268. static int __init pm8xxx_mpp_init(void)
  269. {
  270. return platform_driver_register(&pm8xxx_mpp_driver);
  271. }
  272. postcore_initcall(pm8xxx_mpp_init);
  273. static void __exit pm8xxx_mpp_exit(void)
  274. {
  275. platform_driver_unregister(&pm8xxx_mpp_driver);
  276. }
  277. module_exit(pm8xxx_mpp_exit);
  278. MODULE_LICENSE("GPL v2");
  279. MODULE_DESCRIPTION("PM8XXX MPP driver");
  280. MODULE_VERSION("1.0");
  281. MODULE_ALIAS("platform:" PM8XXX_MPP_DEV_NAME);