onyx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 char *texts[] = { "Line-In", "Microphone" };
  209. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  210. uinfo->count = 1;
  211. uinfo->value.enumerated.items = 2;
  212. if (uinfo->value.enumerated.item > 1)
  213. uinfo->value.enumerated.item = 1;
  214. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  215. return 0;
  216. }
  217. static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_value *ucontrol)
  219. {
  220. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  221. s8 v;
  222. mutex_lock(&onyx->mutex);
  223. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
  224. mutex_unlock(&onyx->mutex);
  225. ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
  226. return 0;
  227. }
  228. static void onyx_set_capture_source(struct onyx *onyx, int mic)
  229. {
  230. s8 v;
  231. mutex_lock(&onyx->mutex);
  232. onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
  233. v &= ~ONYX_ADC_INPUT_MIC;
  234. if (mic)
  235. v |= ONYX_ADC_INPUT_MIC;
  236. onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
  237. mutex_unlock(&onyx->mutex);
  238. }
  239. static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol)
  241. {
  242. if (ucontrol->value.enumerated.item[0] > 1)
  243. return -EINVAL;
  244. onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
  245. ucontrol->value.enumerated.item[0]);
  246. return 1;
  247. }
  248. static struct snd_kcontrol_new capture_source_control = {
  249. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  250. /* If we name this 'Input Source', it properly shows up in
  251. * alsamixer as a selection, * but it's shown under the
  252. * 'Playback' category.
  253. * If I name it 'Capture Source', it shows up in strange
  254. * ways (two bools of which one can be selected at a
  255. * time) but at least it's shown in the 'Capture'
  256. * category.
  257. * I was told that this was due to backward compatibility,
  258. * but I don't understand then why the mangling is *not*
  259. * done when I name it "Input Source".....
  260. */
  261. .name = "Capture Source",
  262. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  263. .info = onyx_snd_capture_source_info,
  264. .get = onyx_snd_capture_source_get,
  265. .put = onyx_snd_capture_source_put,
  266. };
  267. #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
  268. static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
  269. struct snd_ctl_elem_value *ucontrol)
  270. {
  271. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  272. u8 c;
  273. mutex_lock(&onyx->mutex);
  274. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
  275. mutex_unlock(&onyx->mutex);
  276. ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
  277. ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
  278. return 0;
  279. }
  280. static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
  281. struct snd_ctl_elem_value *ucontrol)
  282. {
  283. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  284. u8 v = 0, c = 0;
  285. int err = -EBUSY;
  286. mutex_lock(&onyx->mutex);
  287. if (onyx->analog_locked)
  288. goto out_unlock;
  289. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  290. c = v;
  291. c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
  292. if (!ucontrol->value.integer.value[0])
  293. c |= ONYX_MUTE_LEFT;
  294. if (!ucontrol->value.integer.value[1])
  295. c |= ONYX_MUTE_RIGHT;
  296. err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
  297. out_unlock:
  298. mutex_unlock(&onyx->mutex);
  299. return !err ? (v != c) : err;
  300. }
  301. static struct snd_kcontrol_new mute_control = {
  302. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  303. .name = "Master Playback Switch",
  304. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  305. .info = onyx_snd_mute_info,
  306. .get = onyx_snd_mute_get,
  307. .put = onyx_snd_mute_put,
  308. };
  309. #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
  310. #define FLAG_POLARITY_INVERT 1
  311. #define FLAG_SPDIFLOCK 2
  312. static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
  313. struct snd_ctl_elem_value *ucontrol)
  314. {
  315. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  316. u8 c;
  317. long int pv = kcontrol->private_value;
  318. u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
  319. u8 address = (pv >> 8) & 0xff;
  320. u8 mask = pv & 0xff;
  321. mutex_lock(&onyx->mutex);
  322. onyx_read_register(onyx, address, &c);
  323. mutex_unlock(&onyx->mutex);
  324. ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
  325. return 0;
  326. }
  327. static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
  328. struct snd_ctl_elem_value *ucontrol)
  329. {
  330. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  331. u8 v = 0, c = 0;
  332. int err;
  333. long int pv = kcontrol->private_value;
  334. u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
  335. u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
  336. u8 address = (pv >> 8) & 0xff;
  337. u8 mask = pv & 0xff;
  338. mutex_lock(&onyx->mutex);
  339. if (spdiflock && onyx->spdif_locked) {
  340. /* even if alsamixer doesn't care.. */
  341. err = -EBUSY;
  342. goto out_unlock;
  343. }
  344. onyx_read_register(onyx, address, &v);
  345. c = v;
  346. c &= ~(mask);
  347. if (!!ucontrol->value.integer.value[0] ^ polarity)
  348. c |= mask;
  349. err = onyx_write_register(onyx, address, c);
  350. out_unlock:
  351. mutex_unlock(&onyx->mutex);
  352. return !err ? (v != c) : err;
  353. }
  354. #define SINGLE_BIT(n, type, description, address, mask, flags) \
  355. static struct snd_kcontrol_new n##_control = { \
  356. .iface = SNDRV_CTL_ELEM_IFACE_##type, \
  357. .name = description, \
  358. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  359. .info = onyx_snd_single_bit_info, \
  360. .get = onyx_snd_single_bit_get, \
  361. .put = onyx_snd_single_bit_put, \
  362. .private_value = (flags << 16) | (address << 8) | mask \
  363. }
  364. SINGLE_BIT(spdif,
  365. MIXER,
  366. SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  367. ONYX_REG_DIG_INFO4,
  368. ONYX_SPDIF_ENABLE,
  369. FLAG_SPDIFLOCK);
  370. SINGLE_BIT(ovr1,
  371. MIXER,
  372. "Oversampling Rate",
  373. ONYX_REG_DAC_CONTROL,
  374. ONYX_OVR1,
  375. 0);
  376. SINGLE_BIT(flt0,
  377. MIXER,
  378. "Fast Digital Filter Rolloff",
  379. ONYX_REG_DAC_FILTER,
  380. ONYX_ROLLOFF_FAST,
  381. FLAG_POLARITY_INVERT);
  382. SINGLE_BIT(hpf,
  383. MIXER,
  384. "Highpass Filter",
  385. ONYX_REG_ADC_HPF_BYPASS,
  386. ONYX_HPF_DISABLE,
  387. FLAG_POLARITY_INVERT);
  388. SINGLE_BIT(dm12,
  389. MIXER,
  390. "Digital De-Emphasis",
  391. ONYX_REG_DAC_DEEMPH,
  392. ONYX_DIGDEEMPH_CTRL,
  393. 0);
  394. static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_info *uinfo)
  396. {
  397. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  398. uinfo->count = 1;
  399. return 0;
  400. }
  401. static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
  402. struct snd_ctl_elem_value *ucontrol)
  403. {
  404. /* datasheet page 30, all others are 0 */
  405. ucontrol->value.iec958.status[0] = 0x3e;
  406. ucontrol->value.iec958.status[1] = 0xff;
  407. ucontrol->value.iec958.status[3] = 0x3f;
  408. ucontrol->value.iec958.status[4] = 0x0f;
  409. return 0;
  410. }
  411. static struct snd_kcontrol_new onyx_spdif_mask = {
  412. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  413. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  414. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  415. .info = onyx_spdif_info,
  416. .get = onyx_spdif_mask_get,
  417. };
  418. static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
  419. struct snd_ctl_elem_value *ucontrol)
  420. {
  421. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  422. u8 v;
  423. mutex_lock(&onyx->mutex);
  424. onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
  425. ucontrol->value.iec958.status[0] = v & 0x3e;
  426. onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
  427. ucontrol->value.iec958.status[1] = v;
  428. onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
  429. ucontrol->value.iec958.status[3] = v & 0x3f;
  430. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  431. ucontrol->value.iec958.status[4] = v & 0x0f;
  432. mutex_unlock(&onyx->mutex);
  433. return 0;
  434. }
  435. static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
  436. struct snd_ctl_elem_value *ucontrol)
  437. {
  438. struct onyx *onyx = snd_kcontrol_chip(kcontrol);
  439. u8 v;
  440. mutex_lock(&onyx->mutex);
  441. onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
  442. v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
  443. onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
  444. v = ucontrol->value.iec958.status[1];
  445. onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
  446. onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
  447. v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
  448. onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
  449. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  450. v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
  451. onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
  452. mutex_unlock(&onyx->mutex);
  453. return 1;
  454. }
  455. static struct snd_kcontrol_new onyx_spdif_ctrl = {
  456. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  457. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  458. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  459. .info = onyx_spdif_info,
  460. .get = onyx_spdif_get,
  461. .put = onyx_spdif_put,
  462. };
  463. /* our registers */
  464. static u8 register_map[] = {
  465. ONYX_REG_DAC_ATTEN_LEFT,
  466. ONYX_REG_DAC_ATTEN_RIGHT,
  467. ONYX_REG_CONTROL,
  468. ONYX_REG_DAC_CONTROL,
  469. ONYX_REG_DAC_DEEMPH,
  470. ONYX_REG_DAC_FILTER,
  471. ONYX_REG_DAC_OUTPHASE,
  472. ONYX_REG_ADC_CONTROL,
  473. ONYX_REG_ADC_HPF_BYPASS,
  474. ONYX_REG_DIG_INFO1,
  475. ONYX_REG_DIG_INFO2,
  476. ONYX_REG_DIG_INFO3,
  477. ONYX_REG_DIG_INFO4
  478. };
  479. static u8 initial_values[ARRAY_SIZE(register_map)] = {
  480. 0x80, 0x80, /* muted */
  481. ONYX_MRST | ONYX_SRST, /* but handled specially! */
  482. ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
  483. 0, /* no deemphasis */
  484. ONYX_DAC_FILTER_ALWAYS,
  485. ONYX_OUTPHASE_INVERTED,
  486. (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
  487. ONYX_ADC_HPF_ALWAYS,
  488. (1<<2), /* pcm audio */
  489. 2, /* category: pcm coder */
  490. 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
  491. 1 /* 24 bit depth */
  492. };
  493. /* reset registers of chip, either to initial or to previous values */
  494. static int onyx_register_init(struct onyx *onyx)
  495. {
  496. int i;
  497. u8 val;
  498. u8 regs[sizeof(initial_values)];
  499. if (!onyx->initialised) {
  500. memcpy(regs, initial_values, sizeof(initial_values));
  501. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
  502. return -1;
  503. val &= ~ONYX_SILICONVERSION;
  504. val |= initial_values[3];
  505. regs[3] = val;
  506. } else {
  507. for (i=0; i<sizeof(register_map); i++)
  508. regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
  509. }
  510. for (i=0; i<sizeof(register_map); i++) {
  511. if (onyx_write_register(onyx, register_map[i], regs[i]))
  512. return -1;
  513. }
  514. onyx->initialised = 1;
  515. return 0;
  516. }
  517. static struct transfer_info onyx_transfers[] = {
  518. /* this is first so we can skip it if no input is present...
  519. * No hardware exists with that, but it's here as an example
  520. * of what to do :) */
  521. {
  522. /* analog input */
  523. .formats = SNDRV_PCM_FMTBIT_S8 |
  524. SNDRV_PCM_FMTBIT_S16_BE |
  525. SNDRV_PCM_FMTBIT_S24_BE,
  526. .rates = SNDRV_PCM_RATE_8000_96000,
  527. .transfer_in = 1,
  528. .must_be_clock_source = 0,
  529. .tag = 0,
  530. },
  531. {
  532. /* if analog and digital are currently off, anything should go,
  533. * so this entry describes everything we can do... */
  534. .formats = SNDRV_PCM_FMTBIT_S8 |
  535. SNDRV_PCM_FMTBIT_S16_BE |
  536. SNDRV_PCM_FMTBIT_S24_BE
  537. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  538. | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  539. #endif
  540. ,
  541. .rates = SNDRV_PCM_RATE_8000_96000,
  542. .tag = 0,
  543. },
  544. {
  545. /* analog output */
  546. .formats = SNDRV_PCM_FMTBIT_S8 |
  547. SNDRV_PCM_FMTBIT_S16_BE |
  548. SNDRV_PCM_FMTBIT_S24_BE,
  549. .rates = SNDRV_PCM_RATE_8000_96000,
  550. .transfer_in = 0,
  551. .must_be_clock_source = 0,
  552. .tag = 1,
  553. },
  554. {
  555. /* digital pcm output, also possible for analog out */
  556. .formats = SNDRV_PCM_FMTBIT_S8 |
  557. SNDRV_PCM_FMTBIT_S16_BE |
  558. SNDRV_PCM_FMTBIT_S24_BE,
  559. .rates = SNDRV_PCM_RATE_32000 |
  560. SNDRV_PCM_RATE_44100 |
  561. SNDRV_PCM_RATE_48000,
  562. .transfer_in = 0,
  563. .must_be_clock_source = 0,
  564. .tag = 2,
  565. },
  566. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  567. /* Once alsa gets supports for this kind of thing we can add it... */
  568. {
  569. /* digital compressed output */
  570. .formats = SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
  571. .rates = SNDRV_PCM_RATE_32000 |
  572. SNDRV_PCM_RATE_44100 |
  573. SNDRV_PCM_RATE_48000,
  574. .tag = 2,
  575. },
  576. #endif
  577. {}
  578. };
  579. static int onyx_usable(struct codec_info_item *cii,
  580. struct transfer_info *ti,
  581. struct transfer_info *out)
  582. {
  583. u8 v;
  584. struct onyx *onyx = cii->codec_data;
  585. int spdif_enabled, analog_enabled;
  586. mutex_lock(&onyx->mutex);
  587. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  588. spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
  589. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  590. analog_enabled =
  591. (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
  592. != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
  593. mutex_unlock(&onyx->mutex);
  594. switch (ti->tag) {
  595. case 0: return 1;
  596. case 1: return analog_enabled;
  597. case 2: return spdif_enabled;
  598. }
  599. return 1;
  600. }
  601. static int onyx_prepare(struct codec_info_item *cii,
  602. struct bus_info *bi,
  603. struct snd_pcm_substream *substream)
  604. {
  605. u8 v;
  606. struct onyx *onyx = cii->codec_data;
  607. int err = -EBUSY;
  608. mutex_lock(&onyx->mutex);
  609. #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
  610. if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
  611. /* mute and lock analog output */
  612. onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
  613. if (onyx_write_register(onyx,
  614. ONYX_REG_DAC_CONTROL,
  615. v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
  616. goto out_unlock;
  617. onyx->analog_locked = 1;
  618. err = 0;
  619. goto out_unlock;
  620. }
  621. #endif
  622. switch (substream->runtime->rate) {
  623. case 32000:
  624. case 44100:
  625. case 48000:
  626. /* these rates are ok for all outputs */
  627. /* FIXME: program spdif channel control bits here so that
  628. * userspace doesn't have to if it only plays pcm! */
  629. err = 0;
  630. goto out_unlock;
  631. default:
  632. /* got some rate that the digital output can't do,
  633. * so disable and lock it */
  634. onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
  635. if (onyx_write_register(onyx,
  636. ONYX_REG_DIG_INFO4,
  637. v & ~ONYX_SPDIF_ENABLE))
  638. goto out_unlock;
  639. onyx->spdif_locked = 1;
  640. err = 0;
  641. goto out_unlock;
  642. }
  643. out_unlock:
  644. mutex_unlock(&onyx->mutex);
  645. return err;
  646. }
  647. static int onyx_open(struct codec_info_item *cii,
  648. struct snd_pcm_substream *substream)
  649. {
  650. struct onyx *onyx = cii->codec_data;
  651. mutex_lock(&onyx->mutex);
  652. onyx->open_count++;
  653. mutex_unlock(&onyx->mutex);
  654. return 0;
  655. }
  656. static int onyx_close(struct codec_info_item *cii,
  657. struct snd_pcm_substream *substream)
  658. {
  659. struct onyx *onyx = cii->codec_data;
  660. mutex_lock(&onyx->mutex);
  661. onyx->open_count--;
  662. if (!onyx->open_count)
  663. onyx->spdif_locked = onyx->analog_locked = 0;
  664. mutex_unlock(&onyx->mutex);
  665. return 0;
  666. }
  667. static int onyx_switch_clock(struct codec_info_item *cii,
  668. enum clock_switch what)
  669. {
  670. struct onyx *onyx = cii->codec_data;
  671. mutex_lock(&onyx->mutex);
  672. /* this *MUST* be more elaborate later... */
  673. switch (what) {
  674. case CLOCK_SWITCH_PREPARE_SLAVE:
  675. onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
  676. break;
  677. case CLOCK_SWITCH_SLAVE:
  678. onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
  679. break;
  680. default: /* silence warning */
  681. break;
  682. }
  683. mutex_unlock(&onyx->mutex);
  684. return 0;
  685. }
  686. #ifdef CONFIG_PM
  687. static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
  688. {
  689. struct onyx *onyx = cii->codec_data;
  690. u8 v;
  691. int err = -ENXIO;
  692. mutex_lock(&onyx->mutex);
  693. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
  694. goto out_unlock;
  695. onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
  696. /* Apple does a sleep here but the datasheet says to do it on resume */
  697. err = 0;
  698. out_unlock:
  699. mutex_unlock(&onyx->mutex);
  700. return err;
  701. }
  702. static int onyx_resume(struct codec_info_item *cii)
  703. {
  704. struct onyx *onyx = cii->codec_data;
  705. u8 v;
  706. int err = -ENXIO;
  707. mutex_lock(&onyx->mutex);
  708. /* reset codec */
  709. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  710. msleep(1);
  711. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
  712. msleep(1);
  713. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  714. msleep(1);
  715. /* take codec out of suspend (if it still is after reset) */
  716. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
  717. goto out_unlock;
  718. onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
  719. /* FIXME: should divide by sample rate, but 8k is the lowest we go */
  720. msleep(2205000/8000);
  721. /* reset all values */
  722. onyx_register_init(onyx);
  723. err = 0;
  724. out_unlock:
  725. mutex_unlock(&onyx->mutex);
  726. return err;
  727. }
  728. #endif /* CONFIG_PM */
  729. static struct codec_info onyx_codec_info = {
  730. .transfers = onyx_transfers,
  731. .sysclock_factor = 256,
  732. .bus_factor = 64,
  733. .owner = THIS_MODULE,
  734. .usable = onyx_usable,
  735. .prepare = onyx_prepare,
  736. .open = onyx_open,
  737. .close = onyx_close,
  738. .switch_clock = onyx_switch_clock,
  739. #ifdef CONFIG_PM
  740. .suspend = onyx_suspend,
  741. .resume = onyx_resume,
  742. #endif
  743. };
  744. static int onyx_init_codec(struct aoa_codec *codec)
  745. {
  746. struct onyx *onyx = codec_to_onyx(codec);
  747. struct snd_kcontrol *ctl;
  748. struct codec_info *ci = &onyx_codec_info;
  749. u8 v;
  750. int err;
  751. if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
  752. printk(KERN_ERR PFX "gpios not assigned!!\n");
  753. return -EINVAL;
  754. }
  755. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  756. msleep(1);
  757. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
  758. msleep(1);
  759. onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
  760. msleep(1);
  761. if (onyx_register_init(onyx)) {
  762. printk(KERN_ERR PFX "failed to initialise onyx registers\n");
  763. return -ENODEV;
  764. }
  765. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, onyx, &ops)) {
  766. printk(KERN_ERR PFX "failed to create onyx snd device!\n");
  767. return -ENODEV;
  768. }
  769. /* nothing connected? what a joke! */
  770. if ((onyx->codec.connected & 0xF) == 0)
  771. return -ENOTCONN;
  772. /* if no inputs are present... */
  773. if ((onyx->codec.connected & 0xC) == 0) {
  774. if (!onyx->codec_info)
  775. onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
  776. if (!onyx->codec_info)
  777. return -ENOMEM;
  778. ci = onyx->codec_info;
  779. *ci = onyx_codec_info;
  780. ci->transfers++;
  781. }
  782. /* if no outputs are present... */
  783. if ((onyx->codec.connected & 3) == 0) {
  784. if (!onyx->codec_info)
  785. onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
  786. if (!onyx->codec_info)
  787. return -ENOMEM;
  788. ci = onyx->codec_info;
  789. /* this is fine as there have to be inputs
  790. * if we end up in this part of the code */
  791. *ci = onyx_codec_info;
  792. ci->transfers[1].formats = 0;
  793. }
  794. if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
  795. aoa_get_card(),
  796. ci, onyx)) {
  797. printk(KERN_ERR PFX "error creating onyx pcm\n");
  798. return -ENODEV;
  799. }
  800. #define ADDCTL(n) \
  801. do { \
  802. ctl = snd_ctl_new1(&n, onyx); \
  803. if (ctl) { \
  804. ctl->id.device = \
  805. onyx->codec.soundbus_dev->pcm->device; \
  806. err = aoa_snd_ctl_add(ctl); \
  807. if (err) \
  808. goto error; \
  809. } \
  810. } while (0)
  811. if (onyx->codec.soundbus_dev->pcm) {
  812. /* give the user appropriate controls
  813. * depending on what inputs are connected */
  814. if ((onyx->codec.connected & 0xC) == 0xC)
  815. ADDCTL(capture_source_control);
  816. else if (onyx->codec.connected & 4)
  817. onyx_set_capture_source(onyx, 0);
  818. else
  819. onyx_set_capture_source(onyx, 1);
  820. if (onyx->codec.connected & 0xC)
  821. ADDCTL(inputgain_control);
  822. /* depending on what output is connected,
  823. * give the user appropriate controls */
  824. if (onyx->codec.connected & 1) {
  825. ADDCTL(volume_control);
  826. ADDCTL(mute_control);
  827. ADDCTL(ovr1_control);
  828. ADDCTL(flt0_control);
  829. ADDCTL(hpf_control);
  830. ADDCTL(dm12_control);
  831. /* spdif control defaults to off */
  832. }
  833. if (onyx->codec.connected & 2) {
  834. ADDCTL(onyx_spdif_mask);
  835. ADDCTL(onyx_spdif_ctrl);
  836. }
  837. if ((onyx->codec.connected & 3) == 3)
  838. ADDCTL(spdif_control);
  839. /* if only S/PDIF is connected, enable it unconditionally */
  840. if ((onyx->codec.connected & 3) == 2) {
  841. onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
  842. v |= ONYX_SPDIF_ENABLE;
  843. onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
  844. }
  845. }
  846. #undef ADDCTL
  847. printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
  848. return 0;
  849. error:
  850. onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
  851. snd_device_free(aoa_get_card(), onyx);
  852. return err;
  853. }
  854. static void onyx_exit_codec(struct aoa_codec *codec)
  855. {
  856. struct onyx *onyx = codec_to_onyx(codec);
  857. if (!onyx->codec.soundbus_dev) {
  858. printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
  859. return;
  860. }
  861. onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
  862. }
  863. static int onyx_create(struct i2c_adapter *adapter,
  864. struct device_node *node,
  865. int addr)
  866. {
  867. struct i2c_board_info info;
  868. struct i2c_client *client;
  869. memset(&info, 0, sizeof(struct i2c_board_info));
  870. strlcpy(info.type, "aoa_codec_onyx", I2C_NAME_SIZE);
  871. info.addr = addr;
  872. info.platform_data = node;
  873. client = i2c_new_device(adapter, &info);
  874. if (!client)
  875. return -ENODEV;
  876. /*
  877. * We know the driver is already loaded, so the device should be
  878. * already bound. If not it means binding failed, which suggests
  879. * the device doesn't really exist and should be deleted.
  880. * Ideally this would be replaced by better checks _before_
  881. * instantiating the device.
  882. */
  883. if (!client->driver) {
  884. i2c_unregister_device(client);
  885. return -ENODEV;
  886. }
  887. /*
  888. * Let i2c-core delete that device on driver removal.
  889. * This is safe because i2c-core holds the core_lock mutex for us.
  890. */
  891. list_add_tail(&client->detected, &client->driver->clients);
  892. return 0;
  893. }
  894. static int onyx_i2c_probe(struct i2c_client *client,
  895. const struct i2c_device_id *id)
  896. {
  897. struct device_node *node = client->dev.platform_data;
  898. struct onyx *onyx;
  899. u8 dummy;
  900. onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
  901. if (!onyx)
  902. return -ENOMEM;
  903. mutex_init(&onyx->mutex);
  904. onyx->i2c = client;
  905. i2c_set_clientdata(client, onyx);
  906. /* we try to read from register ONYX_REG_CONTROL
  907. * to check if the codec is present */
  908. if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
  909. printk(KERN_ERR PFX "failed to read control register\n");
  910. goto fail;
  911. }
  912. strlcpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN);
  913. onyx->codec.owner = THIS_MODULE;
  914. onyx->codec.init = onyx_init_codec;
  915. onyx->codec.exit = onyx_exit_codec;
  916. onyx->codec.node = of_node_get(node);
  917. if (aoa_codec_register(&onyx->codec)) {
  918. goto fail;
  919. }
  920. printk(KERN_DEBUG PFX "created and attached onyx instance\n");
  921. return 0;
  922. fail:
  923. i2c_set_clientdata(client, NULL);
  924. kfree(onyx);
  925. return -ENODEV;
  926. }
  927. static int onyx_i2c_attach(struct i2c_adapter *adapter)
  928. {
  929. struct device_node *busnode, *dev = NULL;
  930. struct pmac_i2c_bus *bus;
  931. bus = pmac_i2c_adapter_to_bus(adapter);
  932. if (bus == NULL)
  933. return -ENODEV;
  934. busnode = pmac_i2c_get_bus_node(bus);
  935. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  936. if (of_device_is_compatible(dev, "pcm3052")) {
  937. const u32 *addr;
  938. printk(KERN_DEBUG PFX "found pcm3052\n");
  939. addr = of_get_property(dev, "reg", NULL);
  940. if (!addr)
  941. return -ENODEV;
  942. return onyx_create(adapter, dev, (*addr)>>1);
  943. }
  944. }
  945. /* if that didn't work, try desperate mode for older
  946. * machines that have stuff missing from the device tree */
  947. if (!of_device_is_compatible(busnode, "k2-i2c"))
  948. return -ENODEV;
  949. printk(KERN_DEBUG PFX "found k2-i2c, checking if onyx chip is on it\n");
  950. /* probe both possible addresses for the onyx chip */
  951. if (onyx_create(adapter, NULL, 0x46) == 0)
  952. return 0;
  953. return onyx_create(adapter, NULL, 0x47);
  954. }
  955. static int onyx_i2c_remove(struct i2c_client *client)
  956. {
  957. struct onyx *onyx = i2c_get_clientdata(client);
  958. aoa_codec_unregister(&onyx->codec);
  959. of_node_put(onyx->codec.node);
  960. if (onyx->codec_info)
  961. kfree(onyx->codec_info);
  962. kfree(onyx);
  963. return 0;
  964. }
  965. static const struct i2c_device_id onyx_i2c_id[] = {
  966. { "aoa_codec_onyx", 0 },
  967. { }
  968. };
  969. static struct i2c_driver onyx_driver = {
  970. .driver = {
  971. .name = "aoa_codec_onyx",
  972. .owner = THIS_MODULE,
  973. },
  974. .attach_adapter = onyx_i2c_attach,
  975. .probe = onyx_i2c_probe,
  976. .remove = onyx_i2c_remove,
  977. .id_table = onyx_i2c_id,
  978. };
  979. static int __init onyx_init(void)
  980. {
  981. return i2c_add_driver(&onyx_driver);
  982. }
  983. static void __exit onyx_exit(void)
  984. {
  985. i2c_del_driver(&onyx_driver);
  986. }
  987. module_init(onyx_init);
  988. module_exit(onyx_exit);