ssm2602.c 18 KB

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