twl4030-audio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/i2c/twl.h>
  31. #include <linux/mfd/core.h>
  32. #include <linux/mfd/twl4030-audio.h>
  33. #define TWL4030_AUDIO_CELLS 2
  34. static struct platform_device *twl4030_audio_dev;
  35. struct twl4030_audio_resource {
  36. int request_count;
  37. u8 reg;
  38. u8 mask;
  39. };
  40. struct twl4030_audio {
  41. unsigned int audio_mclk;
  42. struct mutex mutex;
  43. struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
  44. struct mfd_cell cells[TWL4030_AUDIO_CELLS];
  45. };
  46. /*
  47. * Modify the resource, the function returns the content of the register
  48. * after the modification.
  49. */
  50. static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
  51. {
  52. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  53. u8 val;
  54. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  55. audio->resource[id].reg);
  56. if (enable)
  57. val |= audio->resource[id].mask;
  58. else
  59. val &= ~audio->resource[id].mask;
  60. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  61. val, audio->resource[id].reg);
  62. return val;
  63. }
  64. static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
  65. {
  66. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  67. u8 val;
  68. twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
  69. audio->resource[id].reg);
  70. return val;
  71. }
  72. /*
  73. * Enable the resource.
  74. * The function returns with error or the content of the register
  75. */
  76. int twl4030_audio_enable_resource(enum twl4030_audio_res id)
  77. {
  78. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  79. int val;
  80. if (id >= TWL4030_AUDIO_RES_MAX) {
  81. dev_err(&twl4030_audio_dev->dev,
  82. "Invalid resource ID (%u)\n", id);
  83. return -EINVAL;
  84. }
  85. mutex_lock(&audio->mutex);
  86. if (!audio->resource[id].request_count)
  87. /* Resource was disabled, enable it */
  88. val = twl4030_audio_set_resource(id, 1);
  89. else
  90. val = twl4030_audio_get_resource(id);
  91. audio->resource[id].request_count++;
  92. mutex_unlock(&audio->mutex);
  93. return val;
  94. }
  95. EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
  96. /*
  97. * Disable the resource.
  98. * The function returns with error or the content of the register
  99. */
  100. int twl4030_audio_disable_resource(unsigned id)
  101. {
  102. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  103. int val;
  104. if (id >= TWL4030_AUDIO_RES_MAX) {
  105. dev_err(&twl4030_audio_dev->dev,
  106. "Invalid resource ID (%u)\n", id);
  107. return -EINVAL;
  108. }
  109. mutex_lock(&audio->mutex);
  110. if (!audio->resource[id].request_count) {
  111. dev_err(&twl4030_audio_dev->dev,
  112. "Resource has been disabled already (%u)\n", id);
  113. mutex_unlock(&audio->mutex);
  114. return -EPERM;
  115. }
  116. audio->resource[id].request_count--;
  117. if (!audio->resource[id].request_count)
  118. /* Resource can be disabled now */
  119. val = twl4030_audio_set_resource(id, 0);
  120. else
  121. val = twl4030_audio_get_resource(id);
  122. mutex_unlock(&audio->mutex);
  123. return val;
  124. }
  125. EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
  126. unsigned int twl4030_audio_get_mclk(void)
  127. {
  128. struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
  129. return audio->audio_mclk;
  130. }
  131. EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
  132. static int __devinit twl4030_audio_probe(struct platform_device *pdev)
  133. {
  134. struct twl4030_audio *audio;
  135. struct twl4030_audio_data *pdata = pdev->dev.platform_data;
  136. struct mfd_cell *cell = NULL;
  137. int ret, childs = 0;
  138. u8 val;
  139. if (!pdata) {
  140. dev_err(&pdev->dev, "Platform data is missing\n");
  141. return -EINVAL;
  142. }
  143. /* Configure APLL_INFREQ and disable APLL if enabled */
  144. val = 0;
  145. switch (pdata->audio_mclk) {
  146. case 19200000:
  147. val |= TWL4030_APLL_INFREQ_19200KHZ;
  148. break;
  149. case 26000000:
  150. val |= TWL4030_APLL_INFREQ_26000KHZ;
  151. break;
  152. case 38400000:
  153. val |= TWL4030_APLL_INFREQ_38400KHZ;
  154. break;
  155. default:
  156. dev_err(&pdev->dev, "Invalid audio_mclk\n");
  157. return -EINVAL;
  158. }
  159. twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
  160. val, TWL4030_REG_APLL_CTL);
  161. audio = kzalloc(sizeof(struct twl4030_audio), GFP_KERNEL);
  162. if (!audio)
  163. return -ENOMEM;
  164. platform_set_drvdata(pdev, audio);
  165. twl4030_audio_dev = pdev;
  166. mutex_init(&audio->mutex);
  167. audio->audio_mclk = pdata->audio_mclk;
  168. /* Codec power */
  169. audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
  170. audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
  171. /* PLL */
  172. audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
  173. audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
  174. if (pdata->codec) {
  175. cell = &audio->cells[childs];
  176. cell->name = "twl4030-codec";
  177. cell->platform_data = pdata->codec;
  178. cell->pdata_size = sizeof(*pdata->codec);
  179. childs++;
  180. }
  181. if (pdata->vibra) {
  182. cell = &audio->cells[childs];
  183. cell->name = "twl4030-vibra";
  184. cell->platform_data = pdata->vibra;
  185. cell->pdata_size = sizeof(*pdata->vibra);
  186. childs++;
  187. }
  188. if (childs)
  189. ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
  190. childs, NULL, 0);
  191. else {
  192. dev_err(&pdev->dev, "No platform data found for childs\n");
  193. ret = -ENODEV;
  194. }
  195. if (!ret)
  196. return 0;
  197. platform_set_drvdata(pdev, NULL);
  198. kfree(audio);
  199. twl4030_audio_dev = NULL;
  200. return ret;
  201. }
  202. static int __devexit twl4030_audio_remove(struct platform_device *pdev)
  203. {
  204. struct twl4030_audio *audio = platform_get_drvdata(pdev);
  205. mfd_remove_devices(&pdev->dev);
  206. platform_set_drvdata(pdev, NULL);
  207. kfree(audio);
  208. twl4030_audio_dev = NULL;
  209. return 0;
  210. }
  211. MODULE_ALIAS("platform:twl4030-audio");
  212. static struct platform_driver twl4030_audio_driver = {
  213. .probe = twl4030_audio_probe,
  214. .remove = __devexit_p(twl4030_audio_remove),
  215. .driver = {
  216. .owner = THIS_MODULE,
  217. .name = "twl4030-audio",
  218. },
  219. };
  220. module_platform_driver(twl4030_audio_driver);
  221. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  222. MODULE_LICENSE("GPL");