onyx.c 27 KB

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