onyx.c 27 KB

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