twl4030-audio.c 7.3 KB

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