onyx.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * Apple Onboard Audio driver for Onyx codec
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. *
  8. *
  9. * This is a driver for the pcm3052 codec chip (codenamed Onyx)
  10. * that is present in newer Apple hardware (with digital output).
  11. *
  12. * The Onyx codec has the following connections (listed by the bit
  13. * to be used in aoa_codec.connected):
  14. * 0: analog output
  15. * 1: digital output
  16. * 2: line input
  17. * 3: microphone input
  18. * Note that even though I know of no machine that has for example
  19. * the digital output connected but not the analog, I have handled
  20. * all the different cases in the code so that this driver may serve
  21. * as a good example of what to do.
  22. *
  23. * NOTE: This driver assumes that there's at most one chip to be
  24. * used with one alsa card, in form of creating all kinds
  25. * of mixer elements without regard for their existence.
  26. * But snd-aoa assumes that there's at most one card, so
  27. * this means you can only have one onyx on a system. This
  28. * should probably be fixed by changing the assumption of
  29. * having just a single card on a system, and making the
  30. * 'card' pointer accessible to anyone who needs it instead
  31. * of hiding it in the aoa_snd_* functions...
  32. *
  33. */
  34. #include <linux/delay.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  38. MODULE_LICENSE("GPL");
  39. MODULE_DESCRIPTION("pcm3052 (onyx) codec driver for snd-aoa");
  40. #include "onyx.h"
  41. #include "../aoa.h"
  42. #include "../soundbus/soundbus.h"
  43. #define PFX "snd-aoa-codec-onyx: "
  44. struct onyx {
  45. /* cache registers 65 to 80, they are write-only! */
  46. u8 cache[16];
  47. struct i2c_client *i2c;
  48. struct aoa_codec codec;
  49. u32 initialised:1,
  50. spdif_locked:1,
  51. analog_locked:1,
  52. original_mute:2;
  53. int open_count;
  54. struct codec_info *codec_info;
  55. /* mutex serializes concurrent access to the device
  56. * and this structure.
  57. */
  58. struct mutex mutex;
  59. };
  60. #define codec_to_onyx(c) container_of(c, struct onyx, codec)
  61. /* both return 0 if all ok, else on error */
  62. static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
  63. {
  64. s32 v;
  65. if (reg != ONYX_REG_CONTROL) {
  66. *value = onyx->cache[reg-FIRSTREGISTER];
  67. return 0;
  68. }
  69. v = i2c_smbus_read_byte_data(onyx->i2c, reg);
  70. if (v < 0)
  71. return -1;
  72. *value = (u8)v;
  73. onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
  74. return 0;
  75. }
  76. static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
  77. {
  78. int result;
  79. result = i2c_smbus_write_byte_data(onyx->i2c, reg, value);
  80. if (!result)
  81. onyx->cache[reg-FIRSTREGISTER] = value;
  82. return result;
  83. }
  84. /* alsa stuff */
  85. static int onyx_dev_register(struct snd_device *dev)
  86. {
  87. return 0;
  88. }
  89. static struct snd_device_ops ops = {
  90. .dev_register = onyx_dev_register,
  91. };
  92. /* this is necessary because most alsa mixer programs
  93. * can't properly handle the negative range */
  94. #define VOLUME_RANGE_SHIFT 128
  95. static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
  96. struct snd_ctl_elem_info *uinfo)
  97. {
  98. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  99. uinfo->count = 2;
  100. uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
  101. uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
  102. return 0;
  103. }
  104. static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
  105. struct snd_ctl_elem_value *ucontrol)
  106. {
  107. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  108. s8 l, r;
  109. mutex_lock(&onyx->mutex);
  110. onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
  111. onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
  112. mutex_unlock(&onyx->mutex);
  113. ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
  114. ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
  115. return 0;
  116. }
  117. static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
  118. struct snd_ctl_elem_value *ucontrol)
  119. {
  120. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  121. s8 l, r;
  122. if (ucontrol->value.integer.value[0] < -128 + VOLUME_RANGE_SHIFT ||
  123. ucontrol->value.integer.value[0] > -1 + VOLUME_RANGE_SHIFT)
  124. return -EINVAL;
  125. if (ucontrol->value.integer.value[1] < -128 + VOLUME_RANGE_SHIFT ||
  126. ucontrol->value.integer.value[1] > -1 + VOLUME_RANGE_SHIFT)
  127. return -EINVAL;
  128. mutex_lock(&onyx->mutex);
  129. onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
  130. onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
  131. if (l + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[0] &&
  132. r + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[1]) {
  133. mutex_unlock(&onyx->mutex);
  134. return 0;
  135. }
  136. onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_LEFT,
  137. ucontrol->value.integer.value[0]
  138. - VOLUME_RANGE_SHIFT);
  139. onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT,
  140. ucontrol->value.integer.value[1]
  141. - VOLUME_RANGE_SHIFT);
  142. mutex_unlock(&onyx->mutex);
  143. return 1;
  144. }
  145. static struct snd_kcontrol_new volume_control = {
  146. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  147. .name = "Master Playback Volume",
  148. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  149. .info = onyx_snd_vol_info,
  150. .get = onyx_snd_vol_get,
  151. .put = onyx_snd_vol_put,
  152. };
  153. /* like above, this is necessary because a lot
  154. * of alsa mixer programs don't handle ranges
  155. * that don't start at 0 properly.
  156. * even alsamixer is one of them... */
  157. #define INPUTGAIN_RANGE_SHIFT (-3)
  158. static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_info *uinfo)
  160. {
  161. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  162. uinfo->count = 1;
  163. uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
  164. uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
  165. return 0;
  166. }
  167. static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_value *ucontrol)
  169. {
  170. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  171. u8 ig;
  172. mutex_lock(&onyx->mutex);
  173. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
  174. mutex_unlock(&onyx->mutex);
  175. ucontrol->value.integer.value[0] =
  176. (ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
  177. return 0;
  178. }
  179. static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_value *ucontrol)
  181. {
  182. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  183. u8 v, n;
  184. if (ucontrol->value.integer.value[0] < 3 + INPUTGAIN_RANGE_SHIFT ||
  185. ucontrol->value.integer.value[0] > 28 + INPUTGAIN_RANGE_SHIFT)
  186. return -EINVAL;
  187. mutex_lock(&onyx->mutex);
  188. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
  189. n = v;
  190. n &= ~ONYX_ADC_PGA_GAIN_MASK;
  191. n |= (ucontrol->value.integer.value[0] - INPUTGAIN_RANGE_SHIFT)
  192. & ONYX_ADC_PGA_GAIN_MASK;
  193. onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, n);
  194. mutex_unlock(&onyx->mutex);
  195. return n != v;
  196. }
  197. static struct snd_kcontrol_new inputgain_control = {
  198. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  199. .name = "Master Capture Volume",
  200. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  201. .info = onyx_snd_inputgain_info,
  202. .get = onyx_snd_inputgain_get,
  203. .put = onyx_snd_inputgain_put,
  204. };
  205. static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  206. struct snd_ctl_elem_info *uinfo)
  207. {
  208. static const char * const texts[] = { "Line-In", "Microphone" };
  209. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  210. }
  211. static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  212. struct snd_ctl_elem_value *ucontrol)
  213. {
  214. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  215. s8 v;
  216. mutex_lock(&onyx->mutex);
  217. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
  218. mutex_unlock(&onyx->mutex);
  219. ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
  220. return 0;
  221. }
  222. static void onyx_set_capture_source(struct onyx *onyx, int mic)
  223. {
  224. s8 v;
  225. mutex_lock(&onyx->mutex);
  226. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
  227. v &= ~ONYX_ADC_INPUT_MIC;
  228. if (mic)
  229. v |= ONYX_ADC_INPUT_MIC;
  230. onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
  231. mutex_unlock(&onyx->mutex);
  232. }
  233. static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  234. struct snd_ctl_elem_value *ucontrol)
  235. {
  236. if (ucontrol->value.enumerated.item[0] > 1)
  237. return -EINVAL;
  238. onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
  239. ucontrol->value.enumerated.item[0]);
  240. return 1;
  241. }
  242. static struct snd_kcontrol_new capture_source_control = {
  243. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  244. /* If we name this 'Input Source', it properly shows up in
  245. * alsamixer as a selection, * but it's shown under the
  246. * 'Playback' category.
  247. * If I name it 'Capture Source', it shows up in strange
  248. * ways (two bools of which one can be selected at a
  249. * time) but at least it's shown in the 'Capture'
  250. * category.
  251. * I was told that this was due to backward compatibility,
  252. * but I don't understand then why the mangling is *not*
  253. * done when I name it "Input Source".....
  254. */
  255. .name = "Capture Source",
  256. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  257. .info = onyx_snd_capture_source_info,
  258. .get = onyx_snd_capture_source_get,
  259. .put = onyx_snd_capture_source_put,
  260. };
  261. #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
  262. static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
  263. struct snd_ctl_elem_value *ucontrol)
  264. {
  265. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  266. u8 c;
  267. mutex_lock(&onyx->mutex);
  268. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
  269. mutex_unlock(&onyx->mutex);
  270. ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
  271. ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
  272. return 0;
  273. }
  274. static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
  275. struct snd_ctl_elem_value *ucontrol)
  276. {
  277. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  278. u8 v = 0, c = 0;
  279. int err = -EBUSY;
  280. mutex_lock(&onyx->mutex);
  281. if (onyx->analog_locked)
  282. goto out_unlock;
  283. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  284. c = v;
  285. c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
  286. if (!ucontrol->value.integer.value[0])
  287. c |= ONYX_MUTE_LEFT;
  288. if (!ucontrol->value.integer.value[1])
  289. c |= ONYX_MUTE_RIGHT;
  290. err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
  291. out_unlock:
  292. mutex_unlock(&onyx->mutex);
  293. return !err ? (v != c) : err;
  294. }
  295. static struct snd_kcontrol_new mute_control = {
  296. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  297. .name = "Master Playback Switch",
  298. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  299. .info = onyx_snd_mute_info,
  300. .get = onyx_snd_mute_get,
  301. .put = onyx_snd_mute_put,
  302. };
  303. #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
  304. #define FLAG_POLARITY_INVERT 1
  305. #define FLAG_SPDIFLOCK 2
  306. static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
  307. struct snd_ctl_elem_value *ucontrol)
  308. {
  309. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  310. u8 c;
  311. long int pv = kcontrol->private_value;
  312. u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
  313. u8 address = (pv >> 8) & 0xff;
  314. u8 mask = pv & 0xff;
  315. mutex_lock(&onyx->mutex);
  316. onyx_read_register(onyx, address, &c);
  317. mutex_unlock(&onyx->mutex);
  318. ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
  319. return 0;
  320. }
  321. static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
  322. struct snd_ctl_elem_value *ucontrol)
  323. {
  324. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  325. u8 v = 0, c = 0;
  326. int err;
  327. long int pv = kcontrol->private_value;
  328. u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
  329. u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
  330. u8 address = (pv >> 8) & 0xff;
  331. u8 mask = pv & 0xff;
  332. mutex_lock(&onyx->mutex);
  333. if (spdiflock && onyx->spdif_locked) {
  334. /* even if alsamixer doesn't care.. */
  335. err = -EBUSY;
  336. goto out_unlock;
  337. }
  338. onyx_read_register(onyx, address, &v);
  339. c = v;
  340. c &= ~(mask);
  341. if (!!ucontrol->value.integer.value[0] ^ polarity)
  342. c |= mask;
  343. err = onyx_write_register(onyx, address, c);
  344. out_unlock:
  345. mutex_unlock(&onyx->mutex);
  346. return !err ? (v != c) : err;
  347. }
  348. #define SINGLE_BIT(n, type, description, address, mask, flags) \
  349. static struct snd_kcontrol_new n##_control = { \
  350. .iface = SNDRV_CTL_ELEM_IFACE_##type, \
  351. .name = description, \
  352. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  353. .info = onyx_snd_single_bit_info, \
  354. .get = onyx_snd_single_bit_get, \
  355. .put = onyx_snd_single_bit_put, \
  356. .private_value = (flags << 16) | (address << 8) | mask \
  357. }
  358. SINGLE_BIT(spdif,
  359. MIXER,
  360. SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  361. ONYX_REG_DIG_INFO4,
  362. ONYX_SPDIF_ENABLE,
  363. FLAG_SPDIFLOCK);
  364. SINGLE_BIT(ovr1,
  365. MIXER,
  366. "Oversampling Rate",
  367. ONYX_REG_DAC_CONTROL,
  368. ONYX_OVR1,
  369. 0);
  370. SINGLE_BIT(flt0,
  371. MIXER,
  372. "Fast Digital Filter Rolloff",
  373. ONYX_REG_DAC_FILTER,
  374. ONYX_ROLLOFF_FAST,
  375. FLAG_POLARITY_INVERT);
  376. SINGLE_BIT(hpf,
  377. MIXER,
  378. "Highpass Filter",
  379. ONYX_REG_ADC_HPF_BYPASS,
  380. ONYX_HPF_DISABLE,
  381. FLAG_POLARITY_INVERT);
  382. SINGLE_BIT(dm12,
  383. MIXER,
  384. "Digital De-Emphasis",
  385. ONYX_REG_DAC_DEEMPH,
  386. ONYX_DIGDEEMPH_CTRL,
  387. 0);
  388. static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
  389. struct snd_ctl_elem_info *uinfo)
  390. {
  391. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  392. uinfo->count = 1;
  393. return 0;
  394. }
  395. static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
  396. struct snd_ctl_elem_value *ucontrol)
  397. {
  398. /* datasheet page 30, all others are 0 */
  399. ucontrol->value.iec958.status[0] = 0x3e;
  400. ucontrol->value.iec958.status[1] = 0xff;
  401. ucontrol->value.iec958.status[3] = 0x3f;
  402. ucontrol->value.iec958.status[4] = 0x0f;
  403. return 0;
  404. }
  405. static struct snd_kcontrol_new onyx_spdif_mask = {
  406. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  407. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  408. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  409. .info = onyx_spdif_info,
  410. .get = onyx_spdif_mask_get,
  411. };
  412. static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
  413. struct snd_ctl_elem_value *ucontrol)
  414. {
  415. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  416. u8 v;
  417. mutex_lock(&onyx->mutex);
  418. onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
  419. ucontrol->value.iec958.status[0] = v & 0x3e;
  420. onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
  421. ucontrol->value.iec958.status[1] = v;
  422. onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
  423. ucontrol->value.iec958.status[3] = v & 0x3f;
  424. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  425. ucontrol->value.iec958.status[4] = v & 0x0f;
  426. mutex_unlock(&onyx->mutex);
  427. return 0;
  428. }
  429. static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
  430. struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  433. u8 v;
  434. mutex_lock(&onyx->mutex);
  435. onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
  436. v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
  437. onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
  438. v = ucontrol->value.iec958.status[1];
  439. onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
  440. onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
  441. v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
  442. onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
  443. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  444. v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
  445. onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
  446. mutex_unlock(&onyx->mutex);
  447. return 1;
  448. }
  449. static struct snd_kcontrol_new onyx_spdif_ctrl = {
  450. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  451. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  452. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  453. .info = onyx_spdif_info,
  454. .get = onyx_spdif_get,
  455. .put = onyx_spdif_put,
  456. };
  457. /* our registers */
  458. static u8 register_map[] = {
  459. ONYX_REG_DAC_ATTEN_LEFT,
  460. ONYX_REG_DAC_ATTEN_RIGHT,
  461. ONYX_REG_CONTROL,
  462. ONYX_REG_DAC_CONTROL,
  463. ONYX_REG_DAC_DEEMPH,
  464. ONYX_REG_DAC_FILTER,
  465. ONYX_REG_DAC_OUTPHASE,
  466. ONYX_REG_ADC_CONTROL,
  467. ONYX_REG_ADC_HPF_BYPASS,
  468. ONYX_REG_DIG_INFO1,
  469. ONYX_REG_DIG_INFO2,
  470. ONYX_REG_DIG_INFO3,
  471. ONYX_REG_DIG_INFO4
  472. };
  473. static u8 initial_values[ARRAY_SIZE(register_map)] = {
  474. 0x80, 0x80, /* muted */
  475. ONYX_MRST | ONYX_SRST, /* but handled specially! */
  476. ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
  477. 0, /* no deemphasis */
  478. ONYX_DAC_FILTER_ALWAYS,
  479. ONYX_OUTPHASE_INVERTED,
  480. (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
  481. ONYX_ADC_HPF_ALWAYS,
  482. (1<<2), /* pcm audio */
  483. 2, /* category: pcm coder */
  484. 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
  485. 1 /* 24 bit depth */
  486. };
  487. /* reset registers of chip, either to initial or to previous values */
  488. static int onyx_register_init(struct onyx *onyx)
  489. {
  490. int i;
  491. u8 val;
  492. u8 regs[sizeof(initial_values)];
  493. if (!onyx->initialised) {
  494. memcpy(regs, initial_values, sizeof(initial_values));
  495. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
  496. return -1;
  497. val &= ~ONYX_SILICONVERSION;
  498. val |= initial_values[3];
  499. regs[3] = val;
  500. } else {
  501. for (i=0; i<sizeof(register_map); i++)
  502. regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
  503. }
  504. for (i=0; i<sizeof(register_map); i++) {
  505. if (onyx_write_register(onyx, register_map[i], regs[i]))
  506. return -1;
  507. }
  508. onyx->initialised = 1;
  509. return 0;
  510. }
  511. static struct transfer_info onyx_transfers[] = {
  512. /* this is first so we can skip it if no input is present...
  513. * No hardware exists with that, but it's here as an example
  514. * of what to do :) */
  515. {
  516. /* analog input */
  517. .formats = SNDRV_PCM_FMTBIT_S8 |
  518. SNDRV_PCM_FMTBIT_S16_BE |
  519. SNDRV_PCM_FMTBIT_S24_BE,
  520. .rates = SNDRV_PCM_RATE_8000_96000,
  521. .transfer_in = 1,
  522. .must_be_clock_source = 0,
  523. .tag = 0,
  524. },
  525. {
  526. /* if analog and digital are currently off, anything should go,
  527. * so this entry describes everything we can do... */
  528. .formats = SNDRV_PCM_FMTBIT_S8 |
  529. SNDRV_PCM_FMTBIT_S16_BE |
  530. SNDRV_PCM_FMTBIT_S24_BE
  531. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  532. | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  533. #endif
  534. ,
  535. .rates = SNDRV_PCM_RATE_8000_96000,
  536. .tag = 0,
  537. },
  538. {
  539. /* analog output */
  540. .formats = SNDRV_PCM_FMTBIT_S8 |
  541. SNDRV_PCM_FMTBIT_S16_BE |
  542. SNDRV_PCM_FMTBIT_S24_BE,
  543. .rates = SNDRV_PCM_RATE_8000_96000,
  544. .transfer_in = 0,
  545. .must_be_clock_source = 0,
  546. .tag = 1,
  547. },
  548. {
  549. /* digital pcm output, also possible for analog out */
  550. .formats = SNDRV_PCM_FMTBIT_S8 |
  551. SNDRV_PCM_FMTBIT_S16_BE |
  552. SNDRV_PCM_FMTBIT_S24_BE,
  553. .rates = SNDRV_PCM_RATE_32000 |
  554. SNDRV_PCM_RATE_44100 |
  555. SNDRV_PCM_RATE_48000,
  556. .transfer_in = 0,
  557. .must_be_clock_source = 0,
  558. .tag = 2,
  559. },
  560. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  561. /* Once alsa gets supports for this kind of thing we can add it... */
  562. {
  563. /* digital compressed output */
  564. .formats = SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
  565. .rates = SNDRV_PCM_RATE_32000 |
  566. SNDRV_PCM_RATE_44100 |
  567. SNDRV_PCM_RATE_48000,
  568. .tag = 2,
  569. },
  570. #endif
  571. {}
  572. };
  573. static int onyx_usable(struct codec_info_item *cii,
  574. struct transfer_info *ti,
  575. struct transfer_info *out)
  576. {
  577. u8 v;
  578. struct onyx *onyx = cii->codec_data;
  579. int spdif_enabled, analog_enabled;
  580. mutex_lock(&onyx->mutex);
  581. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  582. spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
  583. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  584. analog_enabled =
  585. (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
  586. != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
  587. mutex_unlock(&onyx->mutex);
  588. switch (ti->tag) {
  589. case 0: return 1;
  590. case 1: return analog_enabled;
  591. case 2: return spdif_enabled;
  592. }
  593. return 1;
  594. }
  595. static int onyx_prepare(struct codec_info_item *cii,
  596. struct bus_info *bi,
  597. struct snd_pcm_substream *substream)
  598. {
  599. u8 v;
  600. struct onyx *onyx = cii->codec_data;
  601. int err = -EBUSY;
  602. mutex_lock(&onyx->mutex);
  603. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  604. if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
  605. /* mute and lock analog output */
  606. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  607. if (onyx_write_register(onyx,
  608. ONYX_REG_DAC_CONTROL,
  609. v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
  610. goto out_unlock;
  611. onyx->analog_locked = 1;
  612. err = 0;
  613. goto out_unlock;
  614. }
  615. #endif
  616. switch (substream->runtime->rate) {
  617. case 32000:
  618. case 44100:
  619. case 48000:
  620. /* these rates are ok for all outputs */
  621. /* FIXME: program spdif channel control bits here so that
  622. * userspace doesn't have to if it only plays pcm! */
  623. err = 0;
  624. goto out_unlock;
  625. default:
  626. /* got some rate that the digital output can't do,
  627. * so disable and lock it */
  628. onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
  629. if (onyx_write_register(onyx,
  630. ONYX_REG_DIG_INFO4,
  631. v & ~ONYX_SPDIF_ENABLE))
  632. goto out_unlock;
  633. onyx->spdif_locked = 1;
  634. err = 0;
  635. goto out_unlock;
  636. }
  637. out_unlock:
  638. mutex_unlock(&onyx->mutex);
  639. return err;
  640. }
  641. static int onyx_open(struct codec_info_item *cii,
  642. struct snd_pcm_substream *substream)
  643. {
  644. struct onyx *onyx = cii->codec_data;
  645. mutex_lock(&onyx->mutex);
  646. onyx->open_count++;
  647. mutex_unlock(&onyx->mutex);
  648. return 0;
  649. }
  650. static int onyx_close(struct codec_info_item *cii,
  651. struct snd_pcm_substream *substream)
  652. {
  653. struct onyx *onyx = cii->codec_data;
  654. mutex_lock(&onyx->mutex);
  655. onyx->open_count--;
  656. if (!onyx->open_count)
  657. onyx->spdif_locked = onyx->analog_locked = 0;
  658. mutex_unlock(&onyx->mutex);
  659. return 0;
  660. }
  661. static int onyx_switch_clock(struct codec_info_item *cii,
  662. enum clock_switch what)
  663. {
  664. struct onyx *onyx = cii->codec_data;
  665. mutex_lock(&onyx->mutex);
  666. /* this *MUST* be more elaborate later... */
  667. switch (what) {
  668. case CLOCK_SWITCH_PREPARE_SLAVE:
  669. onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
  670. break;
  671. case CLOCK_SWITCH_SLAVE:
  672. onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
  673. break;
  674. default: /* silence warning */
  675. break;
  676. }
  677. mutex_unlock(&onyx->mutex);
  678. return 0;
  679. }
  680. #ifdef CONFIG_PM
  681. static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
  682. {
  683. struct onyx *onyx = cii->codec_data;
  684. u8 v;
  685. int err = -ENXIO;
  686. mutex_lock(&onyx->mutex);
  687. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
  688. goto out_unlock;
  689. onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
  690. /* Apple does a sleep here but the datasheet says to do it on resume */
  691. err = 0;
  692. out_unlock:
  693. mutex_unlock(&onyx->mutex);
  694. return err;
  695. }
  696. static int onyx_resume(struct codec_info_item *cii)
  697. {
  698. struct onyx *onyx = cii->codec_data;
  699. u8 v;
  700. int err = -ENXIO;
  701. mutex_lock(&onyx->mutex);
  702. /* reset codec */
  703. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  704. msleep(1);
  705. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
  706. msleep(1);
  707. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  708. msleep(1);
  709. /* take codec out of suspend (if it still is after reset) */
  710. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
  711. goto out_unlock;
  712. onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
  713. /* FIXME: should divide by sample rate, but 8k is the lowest we go */
  714. msleep(2205000/8000);
  715. /* reset all values */
  716. onyx_register_init(onyx);
  717. err = 0;
  718. out_unlock:
  719. mutex_unlock(&onyx->mutex);
  720. return err;
  721. }
  722. #endif /* CONFIG_PM */
  723. static struct codec_info onyx_codec_info = {
  724. .transfers = onyx_transfers,
  725. .sysclock_factor = 256,
  726. .bus_factor = 64,
  727. .owner = THIS_MODULE,
  728. .usable = onyx_usable,
  729. .prepare = onyx_prepare,
  730. .open = onyx_open,
  731. .close = onyx_close,
  732. .switch_clock = onyx_switch_clock,
  733. #ifdef CONFIG_PM
  734. .suspend = onyx_suspend,
  735. .resume = onyx_resume,
  736. #endif
  737. };
  738. static int onyx_init_codec(struct aoa_codec *codec)
  739. {
  740. struct onyx *onyx = codec_to_onyx(codec);
  741. struct snd_kcontrol *ctl;
  742. struct codec_info *ci = &onyx_codec_info;
  743. u8 v;
  744. int err;
  745. if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
  746. printk(KERN_ERR PFX "gpios not assigned!!\n");
  747. return -EINVAL;
  748. }
  749. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  750. msleep(1);
  751. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
  752. msleep(1);
  753. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  754. msleep(1);
  755. if (onyx_register_init(onyx)) {
  756. printk(KERN_ERR PFX "failed to initialise onyx registers\n");
  757. return -ENODEV;
  758. }
  759. if (aoa_snd_device_new(SNDRV_DEV_CODEC, onyx, &ops)) {
  760. printk(KERN_ERR PFX "failed to create onyx snd device!\n");
  761. return -ENODEV;
  762. }
  763. /* nothing connected? what a joke! */
  764. if ((onyx->codec.connected & 0xF) == 0)
  765. return -ENOTCONN;
  766. /* if no inputs are present... */
  767. if ((onyx->codec.connected & 0xC) == 0) {
  768. if (!onyx->codec_info)
  769. onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
  770. if (!onyx->codec_info)
  771. return -ENOMEM;
  772. ci = onyx->codec_info;
  773. *ci = onyx_codec_info;
  774. ci->transfers++;
  775. }
  776. /* if no outputs are present... */
  777. if ((onyx->codec.connected & 3) == 0) {
  778. if (!onyx->codec_info)
  779. onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
  780. if (!onyx->codec_info)
  781. return -ENOMEM;
  782. ci = onyx->codec_info;
  783. /* this is fine as there have to be inputs
  784. * if we end up in this part of the code */
  785. *ci = onyx_codec_info;
  786. ci->transfers[1].formats = 0;
  787. }
  788. if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
  789. aoa_get_card(),
  790. ci, onyx)) {
  791. printk(KERN_ERR PFX "error creating onyx pcm\n");
  792. return -ENODEV;
  793. }
  794. #define ADDCTL(n) \
  795. do { \
  796. ctl = snd_ctl_new1(&n, onyx); \
  797. if (ctl) { \
  798. ctl->id.device = \
  799. onyx->codec.soundbus_dev->pcm->device; \
  800. err = aoa_snd_ctl_add(ctl); \
  801. if (err) \
  802. goto error; \
  803. } \
  804. } while (0)
  805. if (onyx->codec.soundbus_dev->pcm) {
  806. /* give the user appropriate controls
  807. * depending on what inputs are connected */
  808. if ((onyx->codec.connected & 0xC) == 0xC)
  809. ADDCTL(capture_source_control);
  810. else if (onyx->codec.connected & 4)
  811. onyx_set_capture_source(onyx, 0);
  812. else
  813. onyx_set_capture_source(onyx, 1);
  814. if (onyx->codec.connected & 0xC)
  815. ADDCTL(inputgain_control);
  816. /* depending on what output is connected,
  817. * give the user appropriate controls */
  818. if (onyx->codec.connected & 1) {
  819. ADDCTL(volume_control);
  820. ADDCTL(mute_control);
  821. ADDCTL(ovr1_control);
  822. ADDCTL(flt0_control);
  823. ADDCTL(hpf_control);
  824. ADDCTL(dm12_control);
  825. /* spdif control defaults to off */
  826. }
  827. if (onyx->codec.connected & 2) {
  828. ADDCTL(onyx_spdif_mask);
  829. ADDCTL(onyx_spdif_ctrl);
  830. }
  831. if ((onyx->codec.connected & 3) == 3)
  832. ADDCTL(spdif_control);
  833. /* if only S/PDIF is connected, enable it unconditionally */
  834. if ((onyx->codec.connected & 3) == 2) {
  835. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  836. v |= ONYX_SPDIF_ENABLE;
  837. onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
  838. }
  839. }
  840. #undef ADDCTL
  841. printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
  842. return 0;
  843. error:
  844. onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
  845. snd_device_free(aoa_get_card(), onyx);
  846. return err;
  847. }
  848. static void onyx_exit_codec(struct aoa_codec *codec)
  849. {
  850. struct onyx *onyx = codec_to_onyx(codec);
  851. if (!onyx->codec.soundbus_dev) {
  852. printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
  853. return;
  854. }
  855. onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
  856. }
  857. static int onyx_i2c_probe(struct i2c_client *client,
  858. const struct i2c_device_id *id)
  859. {
  860. struct device_node *node = client->dev.of_node;
  861. struct onyx *onyx;
  862. u8 dummy;
  863. onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
  864. if (!onyx)
  865. return -ENOMEM;
  866. mutex_init(&onyx->mutex);
  867. onyx->i2c = client;
  868. i2c_set_clientdata(client, onyx);
  869. /* we try to read from register ONYX_REG_CONTROL
  870. * to check if the codec is present */
  871. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
  872. printk(KERN_ERR PFX "failed to read control register\n");
  873. goto fail;
  874. }
  875. strlcpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN);
  876. onyx->codec.owner = THIS_MODULE;
  877. onyx->codec.init = onyx_init_codec;
  878. onyx->codec.exit = onyx_exit_codec;
  879. onyx->codec.node = of_node_get(node);
  880. if (aoa_codec_register(&onyx->codec)) {
  881. goto fail;
  882. }
  883. printk(KERN_DEBUG PFX "created and attached onyx instance\n");
  884. return 0;
  885. fail:
  886. kfree(onyx);
  887. return -ENODEV;
  888. }
  889. static int onyx_i2c_remove(struct i2c_client *client)
  890. {
  891. struct onyx *onyx = i2c_get_clientdata(client);
  892. aoa_codec_unregister(&onyx->codec);
  893. of_node_put(onyx->codec.node);
  894. kfree(onyx->codec_info);
  895. kfree(onyx);
  896. return 0;
  897. }
  898. static const struct i2c_device_id onyx_i2c_id[] = {
  899. { "MAC,pcm3052", 0 },
  900. { }
  901. };
  902. MODULE_DEVICE_TABLE(i2c,onyx_i2c_id);
  903. static struct i2c_driver onyx_driver = {
  904. .driver = {
  905. .name = "aoa_codec_onyx",
  906. },
  907. .probe = onyx_i2c_probe,
  908. .remove = onyx_i2c_remove,
  909. .id_table = onyx_i2c_id,
  910. };
  911. module_i2c_driver(onyx_driver);