ssm2602.c 18 KB

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