ssm2602.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * File: sound/soc/codecs/ssm2602.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Tue June 06 2008
  6. * Description: Driver for ssm2602 sound chip
  7. *
  8. * Modified:
  9. * Copyright 2008 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/pm.h>
  33. #include <linux/i2c.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/slab.h>
  36. #include <sound/core.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/soc.h>
  40. #include <sound/soc-dapm.h>
  41. #include <sound/initval.h>
  42. #include "ssm2602.h"
  43. #define SSM2602_VERSION "0.1"
  44. struct snd_soc_codec_device soc_codec_dev_ssm2602;
  45. /* codec private data */
  46. struct ssm2602_priv {
  47. unsigned int sysclk;
  48. struct snd_pcm_substream *master_substream;
  49. struct snd_pcm_substream *slave_substream;
  50. };
  51. /*
  52. * ssm2602 register cache
  53. * We can't read the ssm2602 register space when we are
  54. * using 2 wire for device control, so we cache them instead.
  55. * There is no point in caching the reset register
  56. */
  57. static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
  58. 0x0017, 0x0017, 0x0079, 0x0079,
  59. 0x0000, 0x0000, 0x0000, 0x000a,
  60. 0x0000, 0x0000
  61. };
  62. /*
  63. * read ssm2602 register cache
  64. */
  65. static inline unsigned int ssm2602_read_reg_cache(struct snd_soc_codec *codec,
  66. unsigned int reg)
  67. {
  68. u16 *cache = codec->reg_cache;
  69. if (reg == SSM2602_RESET)
  70. return 0;
  71. if (reg >= SSM2602_CACHEREGNUM)
  72. return -1;
  73. return cache[reg];
  74. }
  75. /*
  76. * write ssm2602 register cache
  77. */
  78. static inline void ssm2602_write_reg_cache(struct snd_soc_codec *codec,
  79. u16 reg, unsigned int value)
  80. {
  81. u16 *cache = codec->reg_cache;
  82. if (reg >= SSM2602_CACHEREGNUM)
  83. return;
  84. cache[reg] = value;
  85. }
  86. /*
  87. * write to the ssm2602 register space
  88. */
  89. static int ssm2602_write(struct snd_soc_codec *codec, unsigned int reg,
  90. unsigned int value)
  91. {
  92. u8 data[2];
  93. /* data is
  94. * D15..D9 ssm2602 register offset
  95. * D8...D0 register data
  96. */
  97. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  98. data[1] = value & 0x00ff;
  99. ssm2602_write_reg_cache(codec, reg, value);
  100. if (codec->hw_write(codec->control_data, data, 2) == 2)
  101. return 0;
  102. else
  103. return -EIO;
  104. }
  105. #define ssm2602_reset(c) ssm2602_write(c, SSM2602_RESET, 0)
  106. /*Appending several "None"s just for OSS mixer use*/
  107. static const char *ssm2602_input_select[] = {
  108. "Line", "Mic", "None", "None", "None",
  109. "None", "None", "None",
  110. };
  111. static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  112. static const struct soc_enum ssm2602_enum[] = {
  113. SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
  114. SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
  115. };
  116. static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
  117. SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
  118. 0, 127, 0),
  119. SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
  120. 7, 1, 0),
  121. SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
  122. SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
  123. SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
  124. SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
  125. SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
  126. SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
  127. SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
  128. SOC_ENUM("Capture Source", ssm2602_enum[0]),
  129. SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
  130. };
  131. /* Output Mixer */
  132. static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
  133. SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
  134. SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
  135. SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
  136. };
  137. /* Input mux */
  138. static const struct snd_kcontrol_new ssm2602_input_mux_controls =
  139. SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
  140. static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
  141. SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
  142. &ssm2602_output_mixer_controls[0],
  143. ARRAY_SIZE(ssm2602_output_mixer_controls)),
  144. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
  145. SND_SOC_DAPM_OUTPUT("LOUT"),
  146. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  147. SND_SOC_DAPM_OUTPUT("ROUT"),
  148. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  149. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
  150. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
  151. SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
  152. SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
  153. SND_SOC_DAPM_INPUT("MICIN"),
  154. SND_SOC_DAPM_INPUT("RLINEIN"),
  155. SND_SOC_DAPM_INPUT("LLINEIN"),
  156. };
  157. static const struct snd_soc_dapm_route audio_conn[] = {
  158. /* output mixer */
  159. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  160. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  161. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  162. /* outputs */
  163. {"RHPOUT", NULL, "Output Mixer"},
  164. {"ROUT", NULL, "Output Mixer"},
  165. {"LHPOUT", NULL, "Output Mixer"},
  166. {"LOUT", NULL, "Output Mixer"},
  167. /* input mux */
  168. {"Input Mux", "Line", "Line Input"},
  169. {"Input Mux", "Mic", "Mic Bias"},
  170. {"ADC", NULL, "Input Mux"},
  171. /* inputs */
  172. {"Line Input", NULL, "LLINEIN"},
  173. {"Line Input", NULL, "RLINEIN"},
  174. {"Mic Bias", NULL, "MICIN"},
  175. };
  176. static int ssm2602_add_widgets(struct snd_soc_codec *codec)
  177. {
  178. snd_soc_dapm_new_controls(codec, ssm2602_dapm_widgets,
  179. ARRAY_SIZE(ssm2602_dapm_widgets));
  180. snd_soc_dapm_add_routes(codec, audio_conn, ARRAY_SIZE(audio_conn));
  181. return 0;
  182. }
  183. struct _coeff_div {
  184. u32 mclk;
  185. u32 rate;
  186. u16 fs;
  187. u8 sr:4;
  188. u8 bosr:1;
  189. u8 usb:1;
  190. };
  191. /* codec mclk clock divider coefficients */
  192. static const struct _coeff_div coeff_div[] = {
  193. /* 48k */
  194. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  195. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  196. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  197. /* 32k */
  198. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  199. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  200. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  201. /* 8k */
  202. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  203. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  204. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  205. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  206. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  207. /* 96k */
  208. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  209. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  210. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  211. /* 44.1k */
  212. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  213. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  214. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  215. /* 88.2k */
  216. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  217. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  218. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  219. };
  220. static inline int get_coeff(int mclk, int rate)
  221. {
  222. int i;
  223. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  224. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  225. return i;
  226. }
  227. return i;
  228. }
  229. static int ssm2602_hw_params(struct snd_pcm_substream *substream,
  230. struct snd_pcm_hw_params *params,
  231. struct snd_soc_dai *dai)
  232. {
  233. u16 srate;
  234. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  235. struct snd_soc_device *socdev = rtd->socdev;
  236. struct snd_soc_codec *codec = socdev->card->codec;
  237. struct ssm2602_priv *ssm2602 = codec->private_data;
  238. struct i2c_client *i2c = codec->control_data;
  239. u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
  240. int i = get_coeff(ssm2602->sysclk, params_rate(params));
  241. if (substream == ssm2602->slave_substream) {
  242. dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
  243. return 0;
  244. }
  245. /*no match is found*/
  246. if (i == ARRAY_SIZE(coeff_div))
  247. return -EINVAL;
  248. srate = (coeff_div[i].sr << 2) |
  249. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  250. ssm2602_write(codec, SSM2602_ACTIVE, 0);
  251. ssm2602_write(codec, SSM2602_SRATE, srate);
  252. /* bit size */
  253. switch (params_format(params)) {
  254. case SNDRV_PCM_FORMAT_S16_LE:
  255. break;
  256. case SNDRV_PCM_FORMAT_S20_3LE:
  257. iface |= 0x0004;
  258. break;
  259. case SNDRV_PCM_FORMAT_S24_LE:
  260. iface |= 0x0008;
  261. break;
  262. case SNDRV_PCM_FORMAT_S32_LE:
  263. iface |= 0x000c;
  264. break;
  265. }
  266. ssm2602_write(codec, SSM2602_IFACE, iface);
  267. ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
  268. return 0;
  269. }
  270. static int ssm2602_startup(struct snd_pcm_substream *substream,
  271. struct snd_soc_dai *dai)
  272. {
  273. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  274. struct snd_soc_device *socdev = rtd->socdev;
  275. struct snd_soc_codec *codec = socdev->card->codec;
  276. struct ssm2602_priv *ssm2602 = codec->private_data;
  277. struct i2c_client *i2c = codec->control_data;
  278. struct snd_pcm_runtime *master_runtime;
  279. /* The DAI has shared clocks so if we already have a playback or
  280. * capture going then constrain this substream to match it.
  281. * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
  282. */
  283. if (ssm2602->master_substream) {
  284. master_runtime = ssm2602->master_substream->runtime;
  285. dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
  286. master_runtime->sample_bits,
  287. master_runtime->rate);
  288. if (master_runtime->rate != 0)
  289. snd_pcm_hw_constraint_minmax(substream->runtime,
  290. SNDRV_PCM_HW_PARAM_RATE,
  291. master_runtime->rate,
  292. master_runtime->rate);
  293. if (master_runtime->sample_bits != 0)
  294. snd_pcm_hw_constraint_minmax(substream->runtime,
  295. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  296. master_runtime->sample_bits,
  297. master_runtime->sample_bits);
  298. ssm2602->slave_substream = substream;
  299. } else
  300. ssm2602->master_substream = substream;
  301. return 0;
  302. }
  303. static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
  304. struct snd_soc_dai *dai)
  305. {
  306. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  307. struct snd_soc_device *socdev = rtd->socdev;
  308. struct snd_soc_codec *codec = socdev->card->codec;
  309. /* set active */
  310. ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
  311. return 0;
  312. }
  313. static void ssm2602_shutdown(struct snd_pcm_substream *substream,
  314. struct snd_soc_dai *dai)
  315. {
  316. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  317. struct snd_soc_device *socdev = rtd->socdev;
  318. struct snd_soc_codec *codec = socdev->card->codec;
  319. struct ssm2602_priv *ssm2602 = codec->private_data;
  320. /* deactivate */
  321. if (!codec->active)
  322. ssm2602_write(codec, SSM2602_ACTIVE, 0);
  323. if (ssm2602->master_substream == substream)
  324. ssm2602->master_substream = ssm2602->slave_substream;
  325. ssm2602->slave_substream = NULL;
  326. }
  327. static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
  328. {
  329. struct snd_soc_codec *codec = dai->codec;
  330. u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
  331. if (mute)
  332. ssm2602_write(codec, SSM2602_APDIGI,
  333. mute_reg | APDIGI_ENABLE_DAC_MUTE);
  334. else
  335. ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
  336. return 0;
  337. }
  338. static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  339. int clk_id, unsigned int freq, int dir)
  340. {
  341. struct snd_soc_codec *codec = codec_dai->codec;
  342. struct ssm2602_priv *ssm2602 = codec->private_data;
  343. switch (freq) {
  344. case 11289600:
  345. case 12000000:
  346. case 12288000:
  347. case 16934400:
  348. case 18432000:
  349. ssm2602->sysclk = freq;
  350. return 0;
  351. }
  352. return -EINVAL;
  353. }
  354. static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
  355. unsigned int fmt)
  356. {
  357. struct snd_soc_codec *codec = codec_dai->codec;
  358. u16 iface = 0;
  359. /* set master/slave audio interface */
  360. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  361. case SND_SOC_DAIFMT_CBM_CFM:
  362. iface |= 0x0040;
  363. break;
  364. case SND_SOC_DAIFMT_CBS_CFS:
  365. break;
  366. default:
  367. return -EINVAL;
  368. }
  369. /* interface format */
  370. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  371. case SND_SOC_DAIFMT_I2S:
  372. iface |= 0x0002;
  373. break;
  374. case SND_SOC_DAIFMT_RIGHT_J:
  375. break;
  376. case SND_SOC_DAIFMT_LEFT_J:
  377. iface |= 0x0001;
  378. break;
  379. case SND_SOC_DAIFMT_DSP_A:
  380. iface |= 0x0013;
  381. break;
  382. case SND_SOC_DAIFMT_DSP_B:
  383. iface |= 0x0003;
  384. break;
  385. default:
  386. return -EINVAL;
  387. }
  388. /* clock inversion */
  389. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  390. case SND_SOC_DAIFMT_NB_NF:
  391. break;
  392. case SND_SOC_DAIFMT_IB_IF:
  393. iface |= 0x0090;
  394. break;
  395. case SND_SOC_DAIFMT_IB_NF:
  396. iface |= 0x0080;
  397. break;
  398. case SND_SOC_DAIFMT_NB_IF:
  399. iface |= 0x0010;
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. /* set iface */
  405. ssm2602_write(codec, SSM2602_IFACE, iface);
  406. return 0;
  407. }
  408. static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
  409. enum snd_soc_bias_level level)
  410. {
  411. u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
  412. switch (level) {
  413. case SND_SOC_BIAS_ON:
  414. /* vref/mid, osc on, dac unmute */
  415. ssm2602_write(codec, SSM2602_PWR, reg);
  416. break;
  417. case SND_SOC_BIAS_PREPARE:
  418. break;
  419. case SND_SOC_BIAS_STANDBY:
  420. /* everything off except vref/vmid, */
  421. ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
  422. break;
  423. case SND_SOC_BIAS_OFF:
  424. /* everything off, dac mute, inactive */
  425. ssm2602_write(codec, SSM2602_ACTIVE, 0);
  426. ssm2602_write(codec, SSM2602_PWR, 0xffff);
  427. break;
  428. }
  429. codec->bias_level = level;
  430. return 0;
  431. }
  432. #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
  433. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  434. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  435. #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  436. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  437. static struct snd_soc_dai_ops ssm2602_dai_ops = {
  438. .startup = ssm2602_startup,
  439. .prepare = ssm2602_pcm_prepare,
  440. .hw_params = ssm2602_hw_params,
  441. .shutdown = ssm2602_shutdown,
  442. .digital_mute = ssm2602_mute,
  443. .set_sysclk = ssm2602_set_dai_sysclk,
  444. .set_fmt = ssm2602_set_dai_fmt,
  445. };
  446. struct snd_soc_dai ssm2602_dai = {
  447. .name = "SSM2602",
  448. .playback = {
  449. .stream_name = "Playback",
  450. .channels_min = 2,
  451. .channels_max = 2,
  452. .rates = SSM2602_RATES,
  453. .formats = SSM2602_FORMATS,},
  454. .capture = {
  455. .stream_name = "Capture",
  456. .channels_min = 2,
  457. .channels_max = 2,
  458. .rates = SSM2602_RATES,
  459. .formats = SSM2602_FORMATS,},
  460. .ops = &ssm2602_dai_ops,
  461. };
  462. EXPORT_SYMBOL_GPL(ssm2602_dai);
  463. static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state)
  464. {
  465. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  466. struct snd_soc_codec *codec = socdev->card->codec;
  467. ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
  468. return 0;
  469. }
  470. static int ssm2602_resume(struct platform_device *pdev)
  471. {
  472. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  473. struct snd_soc_codec *codec = socdev->card->codec;
  474. int i;
  475. u8 data[2];
  476. u16 *cache = codec->reg_cache;
  477. /* Sync reg_cache with the hardware */
  478. for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
  479. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  480. data[1] = cache[i] & 0x00ff;
  481. codec->hw_write(codec->control_data, data, 2);
  482. }
  483. ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  484. ssm2602_set_bias_level(codec, codec->suspend_bias_level);
  485. return 0;
  486. }
  487. /*
  488. * initialise the ssm2602 driver
  489. * register the mixer and dsp interfaces with the kernel
  490. */
  491. static int ssm2602_init(struct snd_soc_device *socdev)
  492. {
  493. struct snd_soc_codec *codec = socdev->card->codec;
  494. int reg, ret = 0;
  495. codec->name = "SSM2602";
  496. codec->owner = THIS_MODULE;
  497. codec->read = ssm2602_read_reg_cache;
  498. codec->write = ssm2602_write;
  499. codec->set_bias_level = ssm2602_set_bias_level;
  500. codec->dai = &ssm2602_dai;
  501. codec->num_dai = 1;
  502. codec->reg_cache_size = sizeof(ssm2602_reg);
  503. codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg),
  504. GFP_KERNEL);
  505. if (codec->reg_cache == NULL)
  506. return -ENOMEM;
  507. ssm2602_reset(codec);
  508. /* register pcms */
  509. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  510. if (ret < 0) {
  511. pr_err("ssm2602: failed to create pcms\n");
  512. goto pcm_err;
  513. }
  514. /*power on device*/
  515. ssm2602_write(codec, SSM2602_ACTIVE, 0);
  516. /* set the update bits */
  517. reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
  518. ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
  519. reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
  520. ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
  521. reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
  522. ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
  523. reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
  524. ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
  525. /*select Line in as default input*/
  526. ssm2602_write(codec, SSM2602_APANA,
  527. APANA_ENABLE_MIC_BOOST2 | APANA_SELECT_DAC |
  528. APANA_ENABLE_MIC_BOOST);
  529. ssm2602_write(codec, SSM2602_PWR, 0);
  530. snd_soc_add_controls(codec, ssm2602_snd_controls,
  531. ARRAY_SIZE(ssm2602_snd_controls));
  532. ssm2602_add_widgets(codec);
  533. return ret;
  534. pcm_err:
  535. kfree(codec->reg_cache);
  536. return ret;
  537. }
  538. static struct snd_soc_device *ssm2602_socdev;
  539. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  540. /*
  541. * ssm2602 2 wire address is determined by GPIO5
  542. * state during powerup.
  543. * low = 0x1a
  544. * high = 0x1b
  545. */
  546. static int ssm2602_i2c_probe(struct i2c_client *i2c,
  547. const struct i2c_device_id *id)
  548. {
  549. struct snd_soc_device *socdev = ssm2602_socdev;
  550. struct snd_soc_codec *codec = socdev->card->codec;
  551. int ret;
  552. i2c_set_clientdata(i2c, codec);
  553. codec->control_data = i2c;
  554. ret = ssm2602_init(socdev);
  555. if (ret < 0)
  556. pr_err("failed to initialise SSM2602\n");
  557. return ret;
  558. }
  559. static int ssm2602_i2c_remove(struct i2c_client *client)
  560. {
  561. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  562. kfree(codec->reg_cache);
  563. return 0;
  564. }
  565. static const struct i2c_device_id ssm2602_i2c_id[] = {
  566. { "ssm2602", 0 },
  567. { }
  568. };
  569. MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
  570. /* corgi i2c codec control layer */
  571. static struct i2c_driver ssm2602_i2c_driver = {
  572. .driver = {
  573. .name = "SSM2602 I2C Codec",
  574. .owner = THIS_MODULE,
  575. },
  576. .probe = ssm2602_i2c_probe,
  577. .remove = ssm2602_i2c_remove,
  578. .id_table = ssm2602_i2c_id,
  579. };
  580. static int ssm2602_add_i2c_device(struct platform_device *pdev,
  581. const struct ssm2602_setup_data *setup)
  582. {
  583. struct i2c_board_info info;
  584. struct i2c_adapter *adapter;
  585. struct i2c_client *client;
  586. int ret;
  587. ret = i2c_add_driver(&ssm2602_i2c_driver);
  588. if (ret != 0) {
  589. dev_err(&pdev->dev, "can't add i2c driver\n");
  590. return ret;
  591. }
  592. memset(&info, 0, sizeof(struct i2c_board_info));
  593. info.addr = setup->i2c_address;
  594. strlcpy(info.type, "ssm2602", I2C_NAME_SIZE);
  595. adapter = i2c_get_adapter(setup->i2c_bus);
  596. if (!adapter) {
  597. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  598. setup->i2c_bus);
  599. goto err_driver;
  600. }
  601. client = i2c_new_device(adapter, &info);
  602. i2c_put_adapter(adapter);
  603. if (!client) {
  604. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  605. (unsigned int)info.addr);
  606. goto err_driver;
  607. }
  608. return 0;
  609. err_driver:
  610. i2c_del_driver(&ssm2602_i2c_driver);
  611. return -ENODEV;
  612. }
  613. #endif
  614. static int ssm2602_probe(struct platform_device *pdev)
  615. {
  616. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  617. struct ssm2602_setup_data *setup;
  618. struct snd_soc_codec *codec;
  619. struct ssm2602_priv *ssm2602;
  620. int ret = 0;
  621. pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
  622. setup = socdev->codec_data;
  623. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  624. if (codec == NULL)
  625. return -ENOMEM;
  626. ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
  627. if (ssm2602 == NULL) {
  628. kfree(codec);
  629. return -ENOMEM;
  630. }
  631. codec->private_data = ssm2602;
  632. socdev->card->codec = codec;
  633. mutex_init(&codec->mutex);
  634. INIT_LIST_HEAD(&codec->dapm_widgets);
  635. INIT_LIST_HEAD(&codec->dapm_paths);
  636. ssm2602_socdev = socdev;
  637. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  638. if (setup->i2c_address) {
  639. codec->hw_write = (hw_write_t)i2c_master_send;
  640. ret = ssm2602_add_i2c_device(pdev, setup);
  641. }
  642. #else
  643. /* other interfaces */
  644. #endif
  645. return ret;
  646. }
  647. /* remove everything here */
  648. static int ssm2602_remove(struct platform_device *pdev)
  649. {
  650. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  651. struct snd_soc_codec *codec = socdev->card->codec;
  652. if (codec->control_data)
  653. ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
  654. snd_soc_free_pcms(socdev);
  655. snd_soc_dapm_free(socdev);
  656. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  657. i2c_unregister_device(codec->control_data);
  658. i2c_del_driver(&ssm2602_i2c_driver);
  659. #endif
  660. kfree(codec->private_data);
  661. kfree(codec);
  662. return 0;
  663. }
  664. struct snd_soc_codec_device soc_codec_dev_ssm2602 = {
  665. .probe = ssm2602_probe,
  666. .remove = ssm2602_remove,
  667. .suspend = ssm2602_suspend,
  668. .resume = ssm2602_resume,
  669. };
  670. EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602);
  671. static int __init ssm2602_modinit(void)
  672. {
  673. return snd_soc_register_dai(&ssm2602_dai);
  674. }
  675. module_init(ssm2602_modinit);
  676. static void __exit ssm2602_exit(void)
  677. {
  678. snd_soc_unregister_dai(&ssm2602_dai);
  679. }
  680. module_exit(ssm2602_exit);
  681. MODULE_DESCRIPTION("ASoC ssm2602 driver");
  682. MODULE_AUTHOR("Cliff Cai");
  683. MODULE_LICENSE("GPL");