snd-aoa-codec-onyx.c 28 KB

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