88pm805.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #define PM805_CHIP_ID (0x00)
  31. static const struct i2c_device_id pm80x_id_table[] = {
  32. {"88PM805", CHIP_PM805},
  33. {} /* NULL terminated */
  34. };
  35. MODULE_DEVICE_TABLE(i2c, pm80x_id_table);
  36. /* Interrupt Number in 88PM805 */
  37. enum {
  38. PM805_IRQ_LDO_OFF, /*0 */
  39. PM805_IRQ_SRC_DPLL_LOCK, /*1 */
  40. PM805_IRQ_CLIP_FAULT,
  41. PM805_IRQ_MIC_CONFLICT,
  42. PM805_IRQ_HP2_SHRT,
  43. PM805_IRQ_HP1_SHRT, /*5 */
  44. PM805_IRQ_FINE_PLL_FAULT,
  45. PM805_IRQ_RAW_PLL_FAULT,
  46. PM805_IRQ_VOLP_BTN_DET,
  47. PM805_IRQ_VOLM_BTN_DET,
  48. PM805_IRQ_SHRT_BTN_DET, /*10 */
  49. PM805_IRQ_MIC_DET, /*11 */
  50. PM805_MAX_IRQ,
  51. };
  52. static struct resource codec_resources[] = {
  53. {
  54. /* Headset microphone insertion or removal */
  55. .name = "micin",
  56. .start = PM805_IRQ_MIC_DET,
  57. .end = PM805_IRQ_MIC_DET,
  58. .flags = IORESOURCE_IRQ,
  59. },
  60. {
  61. /* Audio short HP1 */
  62. .name = "audio-short1",
  63. .start = PM805_IRQ_HP1_SHRT,
  64. .end = PM805_IRQ_HP1_SHRT,
  65. .flags = IORESOURCE_IRQ,
  66. },
  67. {
  68. /* Audio short HP2 */
  69. .name = "audio-short2",
  70. .start = PM805_IRQ_HP2_SHRT,
  71. .end = PM805_IRQ_HP2_SHRT,
  72. .flags = IORESOURCE_IRQ,
  73. },
  74. };
  75. static struct mfd_cell codec_devs[] = {
  76. {
  77. .name = "88pm80x-codec",
  78. .num_resources = ARRAY_SIZE(codec_resources),
  79. .resources = &codec_resources[0],
  80. .id = -1,
  81. },
  82. };
  83. static struct regmap_irq pm805_irqs[] = {
  84. /* INT0 */
  85. [PM805_IRQ_LDO_OFF] = {
  86. .mask = PM805_INT1_HP1_SHRT,
  87. },
  88. [PM805_IRQ_SRC_DPLL_LOCK] = {
  89. .mask = PM805_INT1_HP2_SHRT,
  90. },
  91. [PM805_IRQ_CLIP_FAULT] = {
  92. .mask = PM805_INT1_MIC_CONFLICT,
  93. },
  94. [PM805_IRQ_MIC_CONFLICT] = {
  95. .mask = PM805_INT1_CLIP_FAULT,
  96. },
  97. [PM805_IRQ_HP2_SHRT] = {
  98. .mask = PM805_INT1_LDO_OFF,
  99. },
  100. [PM805_IRQ_HP1_SHRT] = {
  101. .mask = PM805_INT1_SRC_DPLL_LOCK,
  102. },
  103. /* INT1 */
  104. [PM805_IRQ_FINE_PLL_FAULT] = {
  105. .reg_offset = 1,
  106. .mask = PM805_INT2_MIC_DET,
  107. },
  108. [PM805_IRQ_RAW_PLL_FAULT] = {
  109. .reg_offset = 1,
  110. .mask = PM805_INT2_SHRT_BTN_DET,
  111. },
  112. [PM805_IRQ_VOLP_BTN_DET] = {
  113. .reg_offset = 1,
  114. .mask = PM805_INT2_VOLM_BTN_DET,
  115. },
  116. [PM805_IRQ_VOLM_BTN_DET] = {
  117. .reg_offset = 1,
  118. .mask = PM805_INT2_VOLP_BTN_DET,
  119. },
  120. [PM805_IRQ_SHRT_BTN_DET] = {
  121. .reg_offset = 1,
  122. .mask = PM805_INT2_RAW_PLL_FAULT,
  123. },
  124. [PM805_IRQ_MIC_DET] = {
  125. .reg_offset = 1,
  126. .mask = PM805_INT2_FINE_PLL_FAULT,
  127. },
  128. };
  129. static int __devinit device_irq_init_805(struct pm80x_chip *chip)
  130. {
  131. struct regmap *map = chip->regmap;
  132. unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
  133. int data, mask, ret = -EINVAL;
  134. if (!map || !chip->irq) {
  135. dev_err(chip->dev, "incorrect parameters\n");
  136. return -EINVAL;
  137. }
  138. /*
  139. * irq_mode defines the way of clearing interrupt. it's read-clear by
  140. * default.
  141. */
  142. mask =
  143. PM805_STATUS0_INT_CLEAR | PM805_STATUS0_INV_INT |
  144. PM800_STATUS0_INT_MASK;
  145. data = PM805_STATUS0_INT_CLEAR;
  146. ret = regmap_update_bits(map, PM805_INT_STATUS0, mask, data);
  147. /*
  148. * PM805_INT_STATUS is under 32K clock domain, so need to
  149. * add proper delay before the next I2C register access.
  150. */
  151. msleep(1);
  152. if (ret < 0)
  153. goto out;
  154. ret =
  155. regmap_add_irq_chip(chip->regmap, chip->irq, flags, -1,
  156. chip->regmap_irq_chip, &chip->irq_data);
  157. out:
  158. return ret;
  159. }
  160. static void device_irq_exit_805(struct pm80x_chip *chip)
  161. {
  162. regmap_del_irq_chip(chip->irq, chip->irq_data);
  163. }
  164. static struct regmap_irq_chip pm805_irq_chip = {
  165. .name = "88pm805",
  166. .irqs = pm805_irqs,
  167. .num_irqs = ARRAY_SIZE(pm805_irqs),
  168. .num_regs = 2,
  169. .status_base = PM805_INT_STATUS1,
  170. .mask_base = PM805_INT_MASK1,
  171. .ack_base = PM805_INT_STATUS1,
  172. };
  173. static int __devinit device_805_init(struct pm80x_chip *chip)
  174. {
  175. int ret = 0;
  176. unsigned int val;
  177. struct regmap *map = chip->regmap;
  178. if (!map) {
  179. dev_err(chip->dev, "regmap is invalid\n");
  180. return -EINVAL;
  181. }
  182. ret = regmap_read(map, PM805_CHIP_ID, &val);
  183. if (ret < 0) {
  184. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  185. goto out_irq_init;
  186. }
  187. chip->version = val;
  188. chip->regmap_irq_chip = &pm805_irq_chip;
  189. ret = device_irq_init_805(chip);
  190. if (ret < 0) {
  191. dev_err(chip->dev, "Failed to init pm805 irq!\n");
  192. goto out_irq_init;
  193. }
  194. ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
  195. ARRAY_SIZE(codec_devs), &codec_resources[0], 0);
  196. if (ret < 0) {
  197. dev_err(chip->dev, "Failed to add codec subdev\n");
  198. goto out_codec;
  199. } else
  200. dev_info(chip->dev, "[%s]:Added mfd codec_devs\n", __func__);
  201. return 0;
  202. out_codec:
  203. device_irq_exit_805(chip);
  204. out_irq_init:
  205. return ret;
  206. }
  207. static int __devinit pm805_probe(struct i2c_client *client,
  208. const struct i2c_device_id *id)
  209. {
  210. int ret = 0;
  211. struct pm80x_chip *chip;
  212. struct pm80x_platform_data *pdata = client->dev.platform_data;
  213. ret = pm80x_init(client, id);
  214. if (ret) {
  215. dev_err(&client->dev, "pm805_init fail!\n");
  216. goto out_init;
  217. }
  218. chip = i2c_get_clientdata(client);
  219. ret = device_805_init(chip);
  220. if (ret) {
  221. dev_err(chip->dev, "%s id 0x%x failed!\n", __func__, chip->id);
  222. goto err_805_init;
  223. }
  224. if (pdata->plat_config)
  225. pdata->plat_config(chip, pdata);
  226. err_805_init:
  227. pm80x_deinit(client);
  228. out_init:
  229. return ret;
  230. }
  231. static int __devexit pm805_remove(struct i2c_client *client)
  232. {
  233. struct pm80x_chip *chip = i2c_get_clientdata(client);
  234. mfd_remove_devices(chip->dev);
  235. device_irq_exit_805(chip);
  236. pm80x_deinit(client);
  237. return 0;
  238. }
  239. static struct i2c_driver pm805_driver = {
  240. .driver = {
  241. .name = "88PM80X",
  242. .owner = THIS_MODULE,
  243. .pm = &pm80x_pm_ops,
  244. },
  245. .probe = pm805_probe,
  246. .remove = __devexit_p(pm805_remove),
  247. .id_table = pm80x_id_table,
  248. };
  249. static int __init pm805_i2c_init(void)
  250. {
  251. return i2c_add_driver(&pm805_driver);
  252. }
  253. subsys_initcall(pm805_i2c_init);
  254. static void __exit pm805_i2c_exit(void)
  255. {
  256. i2c_del_driver(&pm805_driver);
  257. }
  258. module_exit(pm805_i2c_exit);
  259. MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM805");
  260. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  261. MODULE_LICENSE("GPL");