88pm805.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Base driver for Marvell 88PM805
  3. *
  4. * Copyright (C) 2012 Marvell International Ltd.
  5. * Haojian Zhuang <[email protected]>
  6. * Joseph(Yossi) Hanin <[email protected]>
  7. * Qiao Zhou <[email protected]>
  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. /* Headset microphone insertion or removal */
  53. DEFINE_RES_IRQ_NAMED(PM805_IRQ_MIC_DET, "micin"),
  54. /* Audio short HP1 */
  55. DEFINE_RES_IRQ_NAMED(PM805_IRQ_HP1_SHRT, "audio-short1"),
  56. /* Audio short HP2 */
  57. DEFINE_RES_IRQ_NAMED(PM805_IRQ_HP2_SHRT, "audio-short2"),
  58. };
  59. static const struct mfd_cell codec_devs[] = {
  60. {
  61. .name = "88pm80x-codec",
  62. .num_resources = ARRAY_SIZE(codec_resources),
  63. .resources = &codec_resources[0],
  64. .id = -1,
  65. },
  66. };
  67. static struct regmap_irq pm805_irqs[] = {
  68. /* INT0 */
  69. [PM805_IRQ_LDO_OFF] = {
  70. .mask = PM805_INT1_HP1_SHRT,
  71. },
  72. [PM805_IRQ_SRC_DPLL_LOCK] = {
  73. .mask = PM805_INT1_HP2_SHRT,
  74. },
  75. [PM805_IRQ_CLIP_FAULT] = {
  76. .mask = PM805_INT1_MIC_CONFLICT,
  77. },
  78. [PM805_IRQ_MIC_CONFLICT] = {
  79. .mask = PM805_INT1_CLIP_FAULT,
  80. },
  81. [PM805_IRQ_HP2_SHRT] = {
  82. .mask = PM805_INT1_LDO_OFF,
  83. },
  84. [PM805_IRQ_HP1_SHRT] = {
  85. .mask = PM805_INT1_SRC_DPLL_LOCK,
  86. },
  87. /* INT1 */
  88. [PM805_IRQ_FINE_PLL_FAULT] = {
  89. .reg_offset = 1,
  90. .mask = PM805_INT2_MIC_DET,
  91. },
  92. [PM805_IRQ_RAW_PLL_FAULT] = {
  93. .reg_offset = 1,
  94. .mask = PM805_INT2_SHRT_BTN_DET,
  95. },
  96. [PM805_IRQ_VOLP_BTN_DET] = {
  97. .reg_offset = 1,
  98. .mask = PM805_INT2_VOLM_BTN_DET,
  99. },
  100. [PM805_IRQ_VOLM_BTN_DET] = {
  101. .reg_offset = 1,
  102. .mask = PM805_INT2_VOLP_BTN_DET,
  103. },
  104. [PM805_IRQ_SHRT_BTN_DET] = {
  105. .reg_offset = 1,
  106. .mask = PM805_INT2_RAW_PLL_FAULT,
  107. },
  108. [PM805_IRQ_MIC_DET] = {
  109. .reg_offset = 1,
  110. .mask = PM805_INT2_FINE_PLL_FAULT,
  111. },
  112. };
  113. static int device_irq_init_805(struct pm80x_chip *chip)
  114. {
  115. struct regmap *map = chip->regmap;
  116. unsigned long flags = IRQF_ONESHOT;
  117. int data, mask, ret = -EINVAL;
  118. if (!map || !chip->irq) {
  119. dev_err(chip->dev, "incorrect parameters\n");
  120. return -EINVAL;
  121. }
  122. /*
  123. * irq_mode defines the way of clearing interrupt. it's read-clear by
  124. * default.
  125. */
  126. mask =
  127. PM805_STATUS0_INT_CLEAR | PM805_STATUS0_INV_INT |
  128. PM800_STATUS0_INT_MASK;
  129. data = PM805_STATUS0_INT_CLEAR;
  130. ret = regmap_update_bits(map, PM805_INT_STATUS0, mask, data);
  131. /*
  132. * PM805_INT_STATUS is under 32K clock domain, so need to
  133. * add proper delay before the next I2C register access.
  134. */
  135. usleep_range(1000, 3000);
  136. if (ret < 0)
  137. goto out;
  138. ret =
  139. regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1,
  140. chip->regmap_irq_chip, &chip->irq_data);
  141. out:
  142. return ret;
  143. }
  144. static void device_irq_exit_805(struct pm80x_chip *chip)
  145. {
  146. regmap_del_irq_chip(chip->irq, chip->irq_data);
  147. }
  148. static struct regmap_irq_chip pm805_irq_chip = {
  149. .name = "88pm805",
  150. .irqs = pm805_irqs,
  151. .num_irqs = ARRAY_SIZE(pm805_irqs),
  152. .num_regs = 2,
  153. .status_base = PM805_INT_STATUS1,
  154. .mask_base = PM805_INT_MASK1,
  155. .ack_base = PM805_INT_STATUS1,
  156. };
  157. static int device_805_init(struct pm80x_chip *chip)
  158. {
  159. int ret = 0;
  160. struct regmap *map = chip->regmap;
  161. if (!map) {
  162. dev_err(chip->dev, "regmap is invalid\n");
  163. return -EINVAL;
  164. }
  165. chip->regmap_irq_chip = &pm805_irq_chip;
  166. ret = device_irq_init_805(chip);
  167. if (ret < 0) {
  168. dev_err(chip->dev, "Failed to init pm805 irq!\n");
  169. goto out_irq_init;
  170. }
  171. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  172. ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
  173. NULL);
  174. if (ret < 0) {
  175. dev_err(chip->dev, "Failed to add codec subdev\n");
  176. goto out_codec;
  177. } else
  178. dev_info(chip->dev, "[%s]:Added mfd codec_devs\n", __func__);
  179. return 0;
  180. out_codec:
  181. device_irq_exit_805(chip);
  182. out_irq_init:
  183. return ret;
  184. }
  185. static int pm805_probe(struct i2c_client *client,
  186. const struct i2c_device_id *id)
  187. {
  188. int ret = 0;
  189. struct pm80x_chip *chip;
  190. struct pm80x_platform_data *pdata = dev_get_platdata(&client->dev);
  191. ret = pm80x_init(client);
  192. if (ret) {
  193. dev_err(&client->dev, "pm805_init fail!\n");
  194. goto out_init;
  195. }
  196. chip = i2c_get_clientdata(client);
  197. ret = device_805_init(chip);
  198. if (ret) {
  199. dev_err(chip->dev, "Failed to initialize 88pm805 devices\n");
  200. goto err_805_init;
  201. }
  202. if (pdata && pdata->plat_config)
  203. pdata->plat_config(chip, pdata);
  204. err_805_init:
  205. pm80x_deinit();
  206. out_init:
  207. return ret;
  208. }
  209. static void pm805_remove(struct i2c_client *client)
  210. {
  211. struct pm80x_chip *chip = i2c_get_clientdata(client);
  212. mfd_remove_devices(chip->dev);
  213. device_irq_exit_805(chip);
  214. pm80x_deinit();
  215. }
  216. static struct i2c_driver pm805_driver = {
  217. .driver = {
  218. .name = "88PM805",
  219. .pm = &pm80x_pm_ops,
  220. },
  221. .probe = pm805_probe,
  222. .remove = pm805_remove,
  223. .id_table = pm80x_id_table,
  224. };
  225. static int __init pm805_i2c_init(void)
  226. {
  227. return i2c_add_driver(&pm805_driver);
  228. }
  229. subsys_initcall(pm805_i2c_init);
  230. static void __exit pm805_i2c_exit(void)
  231. {
  232. i2c_del_driver(&pm805_driver);
  233. }
  234. module_exit(pm805_i2c_exit);
  235. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM805");
  236. MODULE_AUTHOR("Qiao Zhou <[email protected]>");
  237. MODULE_LICENSE("GPL");