ssm2602.c 21 KB

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