wl1273-core.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * MFD driver for wl1273 FM radio and audio codec submodules.
  3. *
  4. * Copyright (C) 2011 Nokia Corporation
  5. * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/mfd/wl1273-core.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #define DRIVER_DESC "WL1273 FM Radio Core"
  26. static const struct i2c_device_id wl1273_driver_id_table[] = {
  27. { WL1273_FM_DRIVER_NAME, 0 },
  28. { }
  29. };
  30. MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
  31. static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
  32. {
  33. struct i2c_client *client = core->client;
  34. u8 b[2];
  35. int r;
  36. r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
  37. if (r != 2) {
  38. dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
  39. return -EREMOTEIO;
  40. }
  41. *value = (u16)b[0] << 8 | b[1];
  42. return 0;
  43. }
  44. static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
  45. {
  46. struct i2c_client *client = core->client;
  47. u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
  48. int r;
  49. r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
  50. if (r) {
  51. dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
  52. return r;
  53. }
  54. return 0;
  55. }
  56. static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
  57. {
  58. struct i2c_client *client = core->client;
  59. struct i2c_msg msg;
  60. int r;
  61. msg.addr = client->addr;
  62. msg.flags = 0;
  63. msg.buf = data;
  64. msg.len = len;
  65. r = i2c_transfer(client->adapter, &msg, 1);
  66. if (r != 1) {
  67. dev_err(&client->dev, "%s: write error.\n", __func__);
  68. return -EREMOTEIO;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * wl1273_fm_set_audio() - Set audio mode.
  74. * @core: A pointer to the device struct.
  75. * @new_mode: The new audio mode.
  76. *
  77. * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
  78. */
  79. static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
  80. {
  81. int r = 0;
  82. if (core->mode == WL1273_MODE_OFF ||
  83. core->mode == WL1273_MODE_SUSPENDED)
  84. return -EPERM;
  85. if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
  86. r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
  87. WL1273_PCM_DEF_MODE);
  88. if (r)
  89. goto out;
  90. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  91. core->i2s_mode);
  92. if (r)
  93. goto out;
  94. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  95. WL1273_AUDIO_ENABLE_I2S);
  96. if (r)
  97. goto out;
  98. } else if (core->mode == WL1273_MODE_RX &&
  99. new_mode == WL1273_AUDIO_ANALOG) {
  100. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
  101. WL1273_AUDIO_ENABLE_ANALOG);
  102. if (r)
  103. goto out;
  104. } else if (core->mode == WL1273_MODE_TX &&
  105. new_mode == WL1273_AUDIO_DIGITAL) {
  106. r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
  107. core->i2s_mode);
  108. if (r)
  109. goto out;
  110. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  111. WL1273_AUDIO_IO_SET_I2S);
  112. if (r)
  113. goto out;
  114. } else if (core->mode == WL1273_MODE_TX &&
  115. new_mode == WL1273_AUDIO_ANALOG) {
  116. r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
  117. WL1273_AUDIO_IO_SET_ANALOG);
  118. if (r)
  119. goto out;
  120. }
  121. core->audio_mode = new_mode;
  122. out:
  123. return r;
  124. }
  125. /**
  126. * wl1273_fm_set_volume() - Set volume.
  127. * @core: A pointer to the device struct.
  128. * @volume: The new volume value.
  129. */
  130. static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
  131. {
  132. int r;
  133. if (volume > WL1273_MAX_VOLUME)
  134. return -EINVAL;
  135. if (core->volume == volume)
  136. return 0;
  137. r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
  138. if (r)
  139. return r;
  140. core->volume = volume;
  141. return 0;
  142. }
  143. static int wl1273_core_probe(struct i2c_client *client,
  144. const struct i2c_device_id *id)
  145. {
  146. struct wl1273_fm_platform_data *pdata = dev_get_platdata(&client->dev);
  147. struct wl1273_core *core;
  148. struct mfd_cell *cell;
  149. int children = 0;
  150. int r = 0;
  151. dev_dbg(&client->dev, "%s\n", __func__);
  152. if (!pdata) {
  153. dev_err(&client->dev, "No platform data.\n");
  154. return -EINVAL;
  155. }
  156. if (!(pdata->children & WL1273_RADIO_CHILD)) {
  157. dev_err(&client->dev, "Cannot function without radio child.\n");
  158. return -EINVAL;
  159. }
  160. core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
  161. if (!core)
  162. return -ENOMEM;
  163. core->pdata = pdata;
  164. core->client = client;
  165. mutex_init(&core->lock);
  166. i2c_set_clientdata(client, core);
  167. dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
  168. cell = &core->cells[children];
  169. cell->name = "wl1273_fm_radio";
  170. cell->platform_data = &core;
  171. cell->pdata_size = sizeof(core);
  172. children++;
  173. core->read = wl1273_fm_read_reg;
  174. core->write = wl1273_fm_write_cmd;
  175. core->write_data = wl1273_fm_write_data;
  176. core->set_audio = wl1273_fm_set_audio;
  177. core->set_volume = wl1273_fm_set_volume;
  178. if (pdata->children & WL1273_CODEC_CHILD) {
  179. cell = &core->cells[children];
  180. dev_dbg(&client->dev, "%s: Have codec.\n", __func__);
  181. cell->name = "wl1273-codec";
  182. cell->platform_data = &core;
  183. cell->pdata_size = sizeof(core);
  184. children++;
  185. }
  186. dev_dbg(&client->dev, "%s: number of children: %d.\n",
  187. __func__, children);
  188. r = devm_mfd_add_devices(&client->dev, -1, core->cells,
  189. children, NULL, 0, NULL);
  190. if (r)
  191. goto err;
  192. return 0;
  193. err:
  194. pdata->free_resources();
  195. dev_dbg(&client->dev, "%s\n", __func__);
  196. return r;
  197. }
  198. static struct i2c_driver wl1273_core_driver = {
  199. .driver = {
  200. .name = WL1273_FM_DRIVER_NAME,
  201. },
  202. .probe = wl1273_core_probe,
  203. .id_table = wl1273_driver_id_table,
  204. };
  205. static int __init wl1273_core_init(void)
  206. {
  207. int r;
  208. r = i2c_add_driver(&wl1273_core_driver);
  209. if (r) {
  210. pr_err(WL1273_FM_DRIVER_NAME
  211. ": driver registration failed\n");
  212. return r;
  213. }
  214. return r;
  215. }
  216. static void __exit wl1273_core_exit(void)
  217. {
  218. i2c_del_driver(&wl1273_core_driver);
  219. }
  220. late_initcall(wl1273_core_init);
  221. module_exit(wl1273_core_exit);
  222. MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
  223. MODULE_DESCRIPTION(DRIVER_DESC);
  224. MODULE_LICENSE("GPL");