88pm805.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Base driver for Marvell 88PM805
  3. *
  4. * Copyright (C) 2012 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. * Joseph(Yossi) Hanin <yhanin@marvell.com>
  7. * Qiao Zhou <zhouqiao@marvell.com>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/irq.h>
  26. #include <linux/mfd/core.h>
  27. #include <linux/mfd/88pm80x.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. static const struct i2c_device_id pm80x_id_table[] = {
  31. {"88PM805", 0},
  32. {} /* NULL terminated */
  33. };
  34. MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
  35. /* Interrupt Number in 88PM805 */
  36. enum {
  37. PM805_IRQ_LDO_OFF, /*0 */
  38. PM805_IRQ_SRC_DPLL_LOCK, /*1 */
  39. PM805_IRQ_CLIP_FAULT,
  40. PM805_IRQ_MIC_CONFLICT,
  41. PM805_IRQ_HP2_SHRT,
  42. PM805_IRQ_HP1_SHRT, /*5 */
  43. PM805_IRQ_FINE_PLL_FAULT,
  44. PM805_IRQ_RAW_PLL_FAULT,
  45. PM805_IRQ_VOLP_BTN_DET,
  46. PM805_IRQ_VOLM_BTN_DET,
  47. PM805_IRQ_SHRT_BTN_DET, /*10 */
  48. PM805_IRQ_MIC_DET, /*11 */
  49. PM805_MAX_IRQ,
  50. };
  51. static struct resource codec_resources[] = {
  52. {
  53. /* Headset microphone insertion or removal */
  54. .name = "micin",
  55. .start = PM805_IRQ_MIC_DET,
  56. .end = PM805_IRQ_MIC_DET,
  57. .flags = IORESOURCE_IRQ,
  58. },
  59. {
  60. /* Audio short HP1 */
  61. .name = "audio-short1",
  62. .start = PM805_IRQ_HP1_SHRT,
  63. .end = PM805_IRQ_HP1_SHRT,
  64. .flags = IORESOURCE_IRQ,
  65. },
  66. {
  67. /* Audio short HP2 */
  68. .name = "audio-short2",
  69. .start = PM805_IRQ_HP2_SHRT,
  70. .end = PM805_IRQ_HP2_SHRT,
  71. .flags = IORESOURCE_IRQ,
  72. },
  73. };
  74. static const struct mfd_cell codec_devs[] = {
  75. {
  76. .name = "88pm80x-codec",
  77. .num_resources = ARRAY_SIZE(codec_resources),
  78. .resources = &codec_resources[0],
  79. .id = -1,
  80. },
  81. };
  82. static struct regmap_irq pm805_irqs[] = {
  83. /* INT0 */
  84. [PM805_IRQ_LDO_OFF] = {
  85. .mask = PM805_INT1_HP1_SHRT,
  86. },
  87. [PM805_IRQ_SRC_DPLL_LOCK] = {
  88. .mask = PM805_INT1_HP2_SHRT,
  89. },
  90. [PM805_IRQ_CLIP_FAULT] = {
  91. .mask = PM805_INT1_MIC_CONFLICT,
  92. },
  93. [PM805_IRQ_MIC_CONFLICT] = {
  94. .mask = PM805_INT1_CLIP_FAULT,
  95. },
  96. [PM805_IRQ_HP2_SHRT] = {
  97. .mask = PM805_INT1_LDO_OFF,
  98. },
  99. [PM805_IRQ_HP1_SHRT] = {
  100. .mask = PM805_INT1_SRC_DPLL_LOCK,
  101. },
  102. /* INT1 */
  103. [PM805_IRQ_FINE_PLL_FAULT] = {
  104. .reg_offset = 1,
  105. .mask = PM805_INT2_MIC_DET,
  106. },
  107. [PM805_IRQ_RAW_PLL_FAULT] = {
  108. .reg_offset = 1,
  109. .mask = PM805_INT2_SHRT_BTN_DET,
  110. },
  111. [PM805_IRQ_VOLP_BTN_DET] = {
  112. .reg_offset = 1,
  113. .mask = PM805_INT2_VOLM_BTN_DET,
  114. },
  115. [PM805_IRQ_VOLM_BTN_DET] = {
  116. .reg_offset = 1,
  117. .mask = PM805_INT2_VOLP_BTN_DET,
  118. },
  119. [PM805_IRQ_SHRT_BTN_DET] = {
  120. .reg_offset = 1,
  121. .mask = PM805_INT2_RAW_PLL_FAULT,
  122. },
  123. [PM805_IRQ_MIC_DET] = {
  124. .reg_offset = 1,
  125. .mask = PM805_INT2_FINE_PLL_FAULT,
  126. },
  127. };
  128. static int device_irq_init_805(struct pm80x_chip *chip)
  129. {
  130. struct regmap *map = chip->regmap;
  131. unsigned long flags = IRQF_ONESHOT;
  132. int data, mask, ret = -EINVAL;
  133. if (!map || !chip->irq) {
  134. dev_err(chip->dev, "incorrect parameters\n");
  135. return -EINVAL;
  136. }
  137. /*
  138. * irq_mode defines the way of clearing interrupt. it's read-clear by
  139. * default.
  140. */
  141. mask =
  142. PM805_STATUS0_INT_CLEAR | PM805_STATUS0_INV_INT |
  143. PM800_STATUS0_INT_MASK;
  144. data = PM805_STATUS0_INT_CLEAR;
  145. ret = regmap_update_bits(map, PM805_INT_STATUS0, mask, data);
  146. /*
  147. * PM805_INT_STATUS is under 32K clock domain, so need to
  148. * add proper delay before the next I2C register access.
  149. */
  150. usleep_range(1000, 3000);
  151. if (ret < 0)
  152. goto out;
  153. ret =
  154. regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1,
  155. chip->regmap_irq_chip, &chip->irq_data);
  156. out:
  157. return ret;
  158. }
  159. static void device_irq_exit_805(struct pm80x_chip *chip)
  160. {
  161. regmap_del_irq_chip(chip->irq, chip->irq_data);
  162. }
  163. static struct regmap_irq_chip pm805_irq_chip = {
  164. .name = "88pm805",
  165. .irqs = pm805_irqs,
  166. .num_irqs = ARRAY_SIZE(pm805_irqs),
  167. .num_regs = 2,
  168. .status_base = PM805_INT_STATUS1,
  169. .mask_base = PM805_INT_MASK1,
  170. .ack_base = PM805_INT_STATUS1,
  171. };
  172. static int device_805_init(struct pm80x_chip *chip)
  173. {
  174. int ret = 0;
  175. struct regmap *map = chip->regmap;
  176. if (!map) {
  177. dev_err(chip->dev, "regmap is invalid\n");
  178. return -EINVAL;
  179. }
  180. chip->regmap_irq_chip = &pm805_irq_chip;
  181. ret = device_irq_init_805(chip);
  182. if (ret < 0) {
  183. dev_err(chip->dev, "Failed to init pm805 irq!\n");
  184. goto out_irq_init;
  185. }
  186. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  187. ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
  188. NULL);
  189. if (ret < 0) {
  190. dev_err(chip->dev, "Failed to add codec subdev\n");
  191. goto out_codec;
  192. } else
  193. dev_info(chip->dev, "[%s]:Added mfd codec_devs\n", __func__);
  194. return 0;
  195. out_codec:
  196. device_irq_exit_805(chip);
  197. out_irq_init:
  198. return ret;
  199. }
  200. static int pm805_probe(struct i2c_client *client,
  201. const struct i2c_device_id *id)
  202. {
  203. int ret = 0;
  204. struct pm80x_chip *chip;
  205. struct pm80x_platform_data *pdata = dev_get_platdata(&client->dev);
  206. ret = pm80x_init(client);
  207. if (ret) {
  208. dev_err(&client->dev, "pm805_init fail!\n");
  209. goto out_init;
  210. }
  211. chip = i2c_get_clientdata(client);
  212. ret = device_805_init(chip);
  213. if (ret) {
  214. dev_err(chip->dev, "Failed to initialize 88pm805 devices\n");
  215. goto err_805_init;
  216. }
  217. if (pdata && pdata->plat_config)
  218. pdata->plat_config(chip, pdata);
  219. err_805_init:
  220. pm80x_deinit();
  221. out_init:
  222. return ret;
  223. }
  224. static int pm805_remove(struct i2c_client *client)
  225. {
  226. struct pm80x_chip *chip = i2c_get_clientdata(client);
  227. mfd_remove_devices(chip->dev);
  228. device_irq_exit_805(chip);
  229. pm80x_deinit();
  230. return 0;
  231. }
  232. static struct i2c_driver pm805_driver = {
  233. .driver = {
  234. .name = "88PM805",
  235. .pm = &pm80x_pm_ops,
  236. },
  237. .probe = pm805_probe,
  238. .remove = pm805_remove,
  239. .id_table = pm80x_id_table,
  240. };
  241. static int __init pm805_i2c_init(void)
  242. {
  243. return i2c_add_driver(&pm805_driver);
  244. }
  245. subsys_initcall(pm805_i2c_init);
  246. static void __exit pm805_i2c_exit(void)
  247. {
  248. i2c_del_driver(&pm805_driver);
  249. }
  250. module_exit(pm805_i2c_exit);
  251. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM805");
  252. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  253. MODULE_LICENSE("GPL");