snd-aoa-codec-onyx.c 28 KB

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