twl4030-audio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MFD driver for twl4030 audio submodule, which contains an audio codec, and
  4. * the vibra control.
  5. *
  6. * Author: Peter Ujfalusi <[email protected]>
  7. *
  8. * Copyright: (C) 2009 Nokia Corporation
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/fs.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/mfd/twl.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/mfd/twl4030-audio.h>
  21. #define TWL4030_AUDIO_CELLS 2
  22. static struct platform_device *twl4030_audio_dev;
  23. struct twl4030_audio_resource {
  24. int request_count;
  25. u8 reg;
  26. u8 mask;
  27. };
  28. struct twl4030_audio {
  29. unsigned int audio_mclk;
  30. struct mutex mutex;
  31. struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
  32. struct mfd_cell cells[TWL4030_AUDIO_CELLS];
  33. };
  34. /*
  35. * Modify the resource, the function returns the content of the register
  36. * after the modification.
  37. */
  38. static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
  39. {
  40. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  41. u8 val;
  42. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  43. audio->resource[id].reg);
  44. if (enable)
  45. val |= audio->resource[id].mask;
  46. else
  47. val &= ~audio->resource[id].mask;
  48. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  49. val, audio->resource[id].reg);
  50. return val;
  51. }
  52. static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
  53. {
  54. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  55. u8 val;
  56. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  57. audio->resource[id].reg);
  58. return val;
  59. }
  60. /*
  61. * Enable the resource.
  62. * The function returns with error or the content of the register
  63. */
  64. int twl4030_audio_enable_resource(enum twl4030_audio_res id)
  65. {
  66. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  67. int val;
  68. if (id >= TWL4030_AUDIO_RES_MAX) {
  69. dev_err(&twl4030_audio_dev->dev,
  70. "Invalid resource ID (%u)\n", id);
  71. return -EINVAL;
  72. }
  73. mutex_lock(&audio->mutex);
  74. if (!audio->resource[id].request_count)
  75. /* Resource was disabled, enable it */
  76. val = twl4030_audio_set_resource(id, 1);
  77. else
  78. val = twl4030_audio_get_resource(id);
  79. audio->resource[id].request_count++;
  80. mutex_unlock(&audio->mutex);
  81. return val;
  82. }
  83. EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
  84. /*
  85. * Disable the resource.
  86. * The function returns with error or the content of the register
  87. */
  88. int twl4030_audio_disable_resource(enum twl4030_audio_res id)
  89. {
  90. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  91. int val;
  92. if (id >= TWL4030_AUDIO_RES_MAX) {
  93. dev_err(&twl4030_audio_dev->dev,
  94. "Invalid resource ID (%u)\n", id);
  95. return -EINVAL;
  96. }
  97. mutex_lock(&audio->mutex);
  98. if (!audio->resource[id].request_count) {
  99. dev_err(&twl4030_audio_dev->dev,
  100. "Resource has been disabled already (%u)\n", id);
  101. mutex_unlock(&audio->mutex);
  102. return -EPERM;
  103. }
  104. audio->resource[id].request_count--;
  105. if (!audio->resource[id].request_count)
  106. /* Resource can be disabled now */
  107. val = twl4030_audio_set_resource(id, 0);
  108. else
  109. val = twl4030_audio_get_resource(id);
  110. mutex_unlock(&audio->mutex);
  111. return val;
  112. }
  113. EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
  114. unsigned int twl4030_audio_get_mclk(void)
  115. {
  116. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  117. return audio->audio_mclk;
  118. }
  119. EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
  120. static bool twl4030_audio_has_codec(struct twl4030_audio_data *pdata,
  121. struct device_node *parent)
  122. {
  123. struct device_node *node;
  124. if (pdata && pdata->codec)
  125. return true;
  126. node = of_get_child_by_name(parent, "codec");
  127. if (node) {
  128. of_node_put(node);
  129. return true;
  130. }
  131. return false;
  132. }
  133. static bool twl4030_audio_has_vibra(struct twl4030_audio_data *pdata,
  134. struct device_node *node)
  135. {
  136. int vibra;
  137. if (pdata && pdata->vibra)
  138. return true;
  139. if (!of_property_read_u32(node, "ti,enable-vibra", &vibra) && vibra)
  140. return true;
  141. return false;
  142. }
  143. static int twl4030_audio_probe(struct platform_device *pdev)
  144. {
  145. struct twl4030_audio *audio;
  146. struct twl4030_audio_data *pdata = dev_get_platdata(&pdev->dev);
  147. struct device_node *node = pdev->dev.of_node;
  148. struct mfd_cell *cell = NULL;
  149. int ret, childs = 0;
  150. u8 val;
  151. if (!pdata && !node) {
  152. dev_err(&pdev->dev, "Platform data is missing\n");
  153. return -EINVAL;
  154. }
  155. audio = devm_kzalloc(&pdev->dev, sizeof(struct twl4030_audio),
  156. GFP_KERNEL);
  157. if (!audio)
  158. return -ENOMEM;
  159. mutex_init(&audio->mutex);
  160. audio->audio_mclk = twl_get_hfclk_rate();
  161. /* Configure APLL_INFREQ and disable APLL if enabled */
  162. switch (audio->audio_mclk) {
  163. case 19200000:
  164. val = TWL4030_APLL_INFREQ_19200KHZ;
  165. break;
  166. case 26000000:
  167. val = TWL4030_APLL_INFREQ_26000KHZ;
  168. break;
  169. case 38400000:
  170. val = TWL4030_APLL_INFREQ_38400KHZ;
  171. break;
  172. default:
  173. dev_err(&pdev->dev, "Invalid audio_mclk\n");
  174. return -EINVAL;
  175. }
  176. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, val, TWL4030_REG_APLL_CTL);
  177. /* Codec power */
  178. audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
  179. audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
  180. /* PLL */
  181. audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
  182. audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
  183. if (twl4030_audio_has_codec(pdata, node)) {
  184. cell = &audio->cells[childs];
  185. cell->name = "twl4030-codec";
  186. if (pdata) {
  187. cell->platform_data = pdata->codec;
  188. cell->pdata_size = sizeof(*pdata->codec);
  189. }
  190. childs++;
  191. }
  192. if (twl4030_audio_has_vibra(pdata, node)) {
  193. cell = &audio->cells[childs];
  194. cell->name = "twl4030-vibra";
  195. if (pdata) {
  196. cell->platform_data = pdata->vibra;
  197. cell->pdata_size = sizeof(*pdata->vibra);
  198. }
  199. childs++;
  200. }
  201. platform_set_drvdata(pdev, audio);
  202. twl4030_audio_dev = pdev;
  203. if (childs)
  204. ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
  205. childs, NULL, 0, NULL);
  206. else {
  207. dev_err(&pdev->dev, "No platform data found for childs\n");
  208. ret = -ENODEV;
  209. }
  210. if (ret)
  211. twl4030_audio_dev = NULL;
  212. return ret;
  213. }
  214. static int twl4030_audio_remove(struct platform_device *pdev)
  215. {
  216. mfd_remove_devices(&pdev->dev);
  217. twl4030_audio_dev = NULL;
  218. return 0;
  219. }
  220. static const struct of_device_id twl4030_audio_of_match[] = {
  221. {.compatible = "ti,twl4030-audio", },
  222. { },
  223. };
  224. MODULE_DEVICE_TABLE(of, twl4030_audio_of_match);
  225. static struct platform_driver twl4030_audio_driver = {
  226. .driver = {
  227. .name = "twl4030-audio",
  228. .of_match_table = twl4030_audio_of_match,
  229. },
  230. .probe = twl4030_audio_probe,
  231. .remove = twl4030_audio_remove,
  232. };
  233. module_platform_driver(twl4030_audio_driver);
  234. MODULE_AUTHOR("Peter Ujfalusi <[email protected]>");
  235. MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
  236. MODULE_LICENSE("GPL");
  237. MODULE_ALIAS("platform:twl4030-audio");