snd-aoa-codec-onyx.c 29 KB

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