snd-aoa-codec-tas.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Apple Onboard Audio driver for tas codec
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. *
  8. * Open questions:
  9. * - How to distinguish between 3004 and versions?
  10. *
  11. * FIXMEs:
  12. * - This codec driver doesn't honour the 'connected'
  13. * property of the aoa_codec struct, hence if
  14. * it is used in machines where not everything is
  15. * connected it will display wrong mixer elements.
  16. * - Driver assumes that the microphone is always
  17. * monaureal and connected to the right channel of
  18. * the input. This should also be a codec-dependent
  19. * flag, maybe the codec should have 3 different
  20. * bits for the three different possibilities how
  21. * it can be hooked up...
  22. * But as long as I don't see any hardware hooked
  23. * up that way...
  24. * - As Apple notes in their code, the tas3004 seems
  25. * to delay the right channel by one sample. You can
  26. * see this when for example recording stereo in
  27. * audacity, or recording the tas output via cable
  28. * on another machine (use a sinus generator or so).
  29. * I tried programming the BiQuads but couldn't
  30. * make the delay work, maybe someone can read the
  31. * datasheet and fix it. The relevant Apple comment
  32. * is in AppleTAS3004Audio.cpp lines 1637 ff. Note
  33. * that their comment describing how they program
  34. * the filters sucks...
  35. *
  36. * Other things:
  37. * - this should actually register *two* aoa_codec
  38. * structs since it has two inputs. Then it must
  39. * use the prepare callback to forbid running the
  40. * secondary output on a different clock.
  41. * Also, whatever bus knows how to do this must
  42. * provide two soundbus_dev devices and the fabric
  43. * must be able to link them correctly.
  44. *
  45. * I don't even know if Apple ever uses the second
  46. * port on the tas3004 though, I don't think their
  47. * i2s controllers can even do it. OTOH, they all
  48. * derive the clocks from common clocks, so it
  49. * might just be possible. The framework allows the
  50. * codec to refine the transfer_info items in the
  51. * usable callback, so we can simply remove the
  52. * rates the second instance is not using when it
  53. * actually is in use.
  54. * Maybe we'll need to make the sound busses have
  55. * a 'clock group id' value so the codec can
  56. * determine if the two outputs can be driven at
  57. * the same time. But that is likely overkill, up
  58. * to the fabric to not link them up incorrectly,
  59. * and up to the hardware designer to not wire
  60. * them up in some weird unusable way.
  61. */
  62. #include <stddef.h>
  63. #include <linux/i2c.h>
  64. #include <asm/pmac_low_i2c.h>
  65. #include <asm/prom.h>
  66. #include <linux/delay.h>
  67. #include <linux/module.h>
  68. #include <linux/mutex.h>
  69. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  70. MODULE_LICENSE("GPL");
  71. MODULE_DESCRIPTION("tas codec driver for snd-aoa");
  72. #include "snd-aoa-codec-tas.h"
  73. #include "snd-aoa-codec-tas-gain-table.h"
  74. #include "snd-aoa-codec-tas-basstreble.h"
  75. #include "../aoa.h"
  76. #include "../soundbus/soundbus.h"
  77. #define PFX "snd-aoa-codec-tas: "
  78. struct tas {
  79. struct aoa_codec codec;
  80. struct i2c_client i2c;
  81. u32 mute_l:1, mute_r:1 ,
  82. controls_created:1 ,
  83. drc_enabled:1,
  84. hw_enabled:1;
  85. u8 cached_volume_l, cached_volume_r;
  86. u8 mixer_l[3], mixer_r[3];
  87. u8 bass, treble;
  88. u8 acr;
  89. int drc_range;
  90. /* protects hardware access against concurrency from
  91. * userspace when hitting controls and during
  92. * codec init/suspend/resume */
  93. struct mutex mtx;
  94. };
  95. static int tas_reset_init(struct tas *tas);
  96. static struct tas *codec_to_tas(struct aoa_codec *codec)
  97. {
  98. return container_of(codec, struct tas, codec);
  99. }
  100. static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
  101. {
  102. if (len == 1)
  103. return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
  104. else
  105. return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
  106. }
  107. static void tas3004_set_drc(struct tas *tas)
  108. {
  109. unsigned char val[6];
  110. if (tas->drc_enabled)
  111. val[0] = 0x50; /* 3:1 above threshold */
  112. else
  113. val[0] = 0x51; /* disabled */
  114. val[1] = 0x02; /* 1:1 below threshold */
  115. if (tas->drc_range > 0xef)
  116. val[2] = 0xef;
  117. else if (tas->drc_range < 0)
  118. val[2] = 0x00;
  119. else
  120. val[2] = tas->drc_range;
  121. val[3] = 0xb0;
  122. val[4] = 0x60;
  123. val[5] = 0xa0;
  124. tas_write_reg(tas, TAS_REG_DRC, 6, val);
  125. }
  126. static void tas_set_treble(struct tas *tas)
  127. {
  128. u8 tmp;
  129. tmp = tas3004_treble(tas->treble);
  130. tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
  131. }
  132. static void tas_set_bass(struct tas *tas)
  133. {
  134. u8 tmp;
  135. tmp = tas3004_bass(tas->bass);
  136. tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
  137. }
  138. static void tas_set_volume(struct tas *tas)
  139. {
  140. u8 block[6];
  141. int tmp;
  142. u8 left, right;
  143. left = tas->cached_volume_l;
  144. right = tas->cached_volume_r;
  145. if (left > 177) left = 177;
  146. if (right > 177) right = 177;
  147. if (tas->mute_l) left = 0;
  148. if (tas->mute_r) right = 0;
  149. /* analysing the volume and mixer tables shows
  150. * that they are similar enough when we shift
  151. * the mixer table down by 4 bits. The error
  152. * is miniscule, in just one item the error
  153. * is 1, at a value of 0x07f17b (mixer table
  154. * value is 0x07f17a) */
  155. tmp = tas_gaintable[left];
  156. block[0] = tmp>>20;
  157. block[1] = tmp>>12;
  158. block[2] = tmp>>4;
  159. tmp = tas_gaintable[right];
  160. block[3] = tmp>>20;
  161. block[4] = tmp>>12;
  162. block[5] = tmp>>4;
  163. tas_write_reg(tas, TAS_REG_VOL, 6, block);
  164. }
  165. static void tas_set_mixer(struct tas *tas)
  166. {
  167. u8 block[9];
  168. int tmp, i;
  169. u8 val;
  170. for (i=0;i<3;i++) {
  171. val = tas->mixer_l[i];
  172. if (val > 177) val = 177;
  173. tmp = tas_gaintable[val];
  174. block[3*i+0] = tmp>>16;
  175. block[3*i+1] = tmp>>8;
  176. block[3*i+2] = tmp;
  177. }
  178. tas_write_reg(tas, TAS_REG_LMIX, 9, block);
  179. for (i=0;i<3;i++) {
  180. val = tas->mixer_r[i];
  181. if (val > 177) val = 177;
  182. tmp = tas_gaintable[val];
  183. block[3*i+0] = tmp>>16;
  184. block[3*i+1] = tmp>>8;
  185. block[3*i+2] = tmp;
  186. }
  187. tas_write_reg(tas, TAS_REG_RMIX, 9, block);
  188. }
  189. /* alsa stuff */
  190. static int tas_dev_register(struct snd_device *dev)
  191. {
  192. return 0;
  193. }
  194. static struct snd_device_ops ops = {
  195. .dev_register = tas_dev_register,
  196. };
  197. static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
  198. struct snd_ctl_elem_info *uinfo)
  199. {
  200. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  201. uinfo->count = 2;
  202. uinfo->value.integer.min = 0;
  203. uinfo->value.integer.max = 177;
  204. return 0;
  205. }
  206. static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. struct tas *tas = snd_kcontrol_chip(kcontrol);
  210. mutex_lock(&tas->mtx);
  211. ucontrol->value.integer.value[0] = tas->cached_volume_l;
  212. ucontrol->value.integer.value[1] = tas->cached_volume_r;
  213. mutex_unlock(&tas->mtx);
  214. return 0;
  215. }
  216. static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_value *ucontrol)
  218. {
  219. struct tas *tas = snd_kcontrol_chip(kcontrol);
  220. mutex_lock(&tas->mtx);
  221. if (tas->cached_volume_l == ucontrol->value.integer.value[0]
  222. && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
  223. mutex_unlock(&tas->mtx);
  224. return 0;
  225. }
  226. tas->cached_volume_l = ucontrol->value.integer.value[0];
  227. tas->cached_volume_r = ucontrol->value.integer.value[1];
  228. if (tas->hw_enabled)
  229. tas_set_volume(tas);
  230. mutex_unlock(&tas->mtx);
  231. return 1;
  232. }
  233. static struct snd_kcontrol_new volume_control = {
  234. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  235. .name = "Master Playback Volume",
  236. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  237. .info = tas_snd_vol_info,
  238. .get = tas_snd_vol_get,
  239. .put = tas_snd_vol_put,
  240. };
  241. static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_info *uinfo)
  243. {
  244. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  245. uinfo->count = 2;
  246. uinfo->value.integer.min = 0;
  247. uinfo->value.integer.max = 1;
  248. return 0;
  249. }
  250. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  251. struct snd_ctl_elem_value *ucontrol)
  252. {
  253. struct tas *tas = snd_kcontrol_chip(kcontrol);
  254. mutex_lock(&tas->mtx);
  255. ucontrol->value.integer.value[0] = !tas->mute_l;
  256. ucontrol->value.integer.value[1] = !tas->mute_r;
  257. mutex_unlock(&tas->mtx);
  258. return 0;
  259. }
  260. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  261. struct snd_ctl_elem_value *ucontrol)
  262. {
  263. struct tas *tas = snd_kcontrol_chip(kcontrol);
  264. mutex_lock(&tas->mtx);
  265. if (tas->mute_l == !ucontrol->value.integer.value[0]
  266. && tas->mute_r == !ucontrol->value.integer.value[1]) {
  267. mutex_unlock(&tas->mtx);
  268. return 0;
  269. }
  270. tas->mute_l = !ucontrol->value.integer.value[0];
  271. tas->mute_r = !ucontrol->value.integer.value[1];
  272. if (tas->hw_enabled)
  273. tas_set_volume(tas);
  274. mutex_unlock(&tas->mtx);
  275. return 1;
  276. }
  277. static struct snd_kcontrol_new mute_control = {
  278. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  279. .name = "Master Playback Switch",
  280. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  281. .info = tas_snd_mute_info,
  282. .get = tas_snd_mute_get,
  283. .put = tas_snd_mute_put,
  284. };
  285. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  286. struct snd_ctl_elem_info *uinfo)
  287. {
  288. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  289. uinfo->count = 2;
  290. uinfo->value.integer.min = 0;
  291. uinfo->value.integer.max = 177;
  292. return 0;
  293. }
  294. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  295. struct snd_ctl_elem_value *ucontrol)
  296. {
  297. struct tas *tas = snd_kcontrol_chip(kcontrol);
  298. int idx = kcontrol->private_value;
  299. mutex_lock(&tas->mtx);
  300. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  301. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  302. mutex_unlock(&tas->mtx);
  303. return 0;
  304. }
  305. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  306. struct snd_ctl_elem_value *ucontrol)
  307. {
  308. struct tas *tas = snd_kcontrol_chip(kcontrol);
  309. int idx = kcontrol->private_value;
  310. mutex_lock(&tas->mtx);
  311. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  312. && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
  313. mutex_unlock(&tas->mtx);
  314. return 0;
  315. }
  316. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  317. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  318. if (tas->hw_enabled)
  319. tas_set_mixer(tas);
  320. mutex_unlock(&tas->mtx);
  321. return 1;
  322. }
  323. #define MIXER_CONTROL(n,descr,idx) \
  324. static struct snd_kcontrol_new n##_control = { \
  325. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  326. .name = descr " Playback Volume", \
  327. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  328. .info = tas_snd_mixer_info, \
  329. .get = tas_snd_mixer_get, \
  330. .put = tas_snd_mixer_put, \
  331. .private_value = idx, \
  332. }
  333. MIXER_CONTROL(pcm1, "PCM", 0);
  334. MIXER_CONTROL(monitor, "Monitor", 2);
  335. static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
  336. struct snd_ctl_elem_info *uinfo)
  337. {
  338. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  339. uinfo->count = 1;
  340. uinfo->value.integer.min = 0;
  341. uinfo->value.integer.max = TAS3004_DRC_MAX;
  342. return 0;
  343. }
  344. static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
  345. struct snd_ctl_elem_value *ucontrol)
  346. {
  347. struct tas *tas = snd_kcontrol_chip(kcontrol);
  348. mutex_lock(&tas->mtx);
  349. ucontrol->value.integer.value[0] = tas->drc_range;
  350. mutex_unlock(&tas->mtx);
  351. return 0;
  352. }
  353. static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_value *ucontrol)
  355. {
  356. struct tas *tas = snd_kcontrol_chip(kcontrol);
  357. mutex_lock(&tas->mtx);
  358. if (tas->drc_range == ucontrol->value.integer.value[0]) {
  359. mutex_unlock(&tas->mtx);
  360. return 0;
  361. }
  362. tas->drc_range = ucontrol->value.integer.value[0];
  363. if (tas->hw_enabled)
  364. tas3004_set_drc(tas);
  365. mutex_unlock(&tas->mtx);
  366. return 1;
  367. }
  368. static struct snd_kcontrol_new drc_range_control = {
  369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  370. .name = "DRC Range",
  371. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  372. .info = tas_snd_drc_range_info,
  373. .get = tas_snd_drc_range_get,
  374. .put = tas_snd_drc_range_put,
  375. };
  376. static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol,
  377. struct snd_ctl_elem_info *uinfo)
  378. {
  379. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  380. uinfo->count = 1;
  381. uinfo->value.integer.min = 0;
  382. uinfo->value.integer.max = 1;
  383. return 0;
  384. }
  385. static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
  386. struct snd_ctl_elem_value *ucontrol)
  387. {
  388. struct tas *tas = snd_kcontrol_chip(kcontrol);
  389. mutex_lock(&tas->mtx);
  390. ucontrol->value.integer.value[0] = tas->drc_enabled;
  391. mutex_unlock(&tas->mtx);
  392. return 0;
  393. }
  394. static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct tas *tas = snd_kcontrol_chip(kcontrol);
  398. mutex_lock(&tas->mtx);
  399. if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
  400. mutex_unlock(&tas->mtx);
  401. return 0;
  402. }
  403. tas->drc_enabled = ucontrol->value.integer.value[0];
  404. if (tas->hw_enabled)
  405. tas3004_set_drc(tas);
  406. mutex_unlock(&tas->mtx);
  407. return 1;
  408. }
  409. static struct snd_kcontrol_new drc_switch_control = {
  410. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  411. .name = "DRC Range Switch",
  412. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  413. .info = tas_snd_drc_switch_info,
  414. .get = tas_snd_drc_switch_get,
  415. .put = tas_snd_drc_switch_put,
  416. };
  417. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  418. struct snd_ctl_elem_info *uinfo)
  419. {
  420. static char *texts[] = { "Line-In", "Microphone" };
  421. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  422. uinfo->count = 1;
  423. uinfo->value.enumerated.items = 2;
  424. if (uinfo->value.enumerated.item > 1)
  425. uinfo->value.enumerated.item = 1;
  426. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  427. return 0;
  428. }
  429. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  430. struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct tas *tas = snd_kcontrol_chip(kcontrol);
  433. mutex_lock(&tas->mtx);
  434. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  435. mutex_unlock(&tas->mtx);
  436. return 0;
  437. }
  438. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  439. struct snd_ctl_elem_value *ucontrol)
  440. {
  441. struct tas *tas = snd_kcontrol_chip(kcontrol);
  442. int oldacr;
  443. mutex_lock(&tas->mtx);
  444. oldacr = tas->acr;
  445. /*
  446. * Despite what the data sheet says in one place, the
  447. * TAS_ACR_B_MONAUREAL bit forces mono output even when
  448. * input A (line in) is selected.
  449. */
  450. tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
  451. if (ucontrol->value.enumerated.item[0])
  452. tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
  453. TAS_ACR_B_MON_SEL_RIGHT;
  454. if (oldacr == tas->acr) {
  455. mutex_unlock(&tas->mtx);
  456. return 0;
  457. }
  458. if (tas->hw_enabled)
  459. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  460. mutex_unlock(&tas->mtx);
  461. return 1;
  462. }
  463. static struct snd_kcontrol_new capture_source_control = {
  464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  465. /* If we name this 'Input Source', it properly shows up in
  466. * alsamixer as a selection, * but it's shown under the
  467. * 'Playback' category.
  468. * If I name it 'Capture Source', it shows up in strange
  469. * ways (two bools of which one can be selected at a
  470. * time) but at least it's shown in the 'Capture'
  471. * category.
  472. * I was told that this was due to backward compatibility,
  473. * but I don't understand then why the mangling is *not*
  474. * done when I name it "Input Source".....
  475. */
  476. .name = "Capture Source",
  477. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  478. .info = tas_snd_capture_source_info,
  479. .get = tas_snd_capture_source_get,
  480. .put = tas_snd_capture_source_put,
  481. };
  482. static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_info *uinfo)
  484. {
  485. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  486. uinfo->count = 1;
  487. uinfo->value.integer.min = TAS3004_TREBLE_MIN;
  488. uinfo->value.integer.max = TAS3004_TREBLE_MAX;
  489. return 0;
  490. }
  491. static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
  492. struct snd_ctl_elem_value *ucontrol)
  493. {
  494. struct tas *tas = snd_kcontrol_chip(kcontrol);
  495. mutex_lock(&tas->mtx);
  496. ucontrol->value.integer.value[0] = tas->treble;
  497. mutex_unlock(&tas->mtx);
  498. return 0;
  499. }
  500. static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
  501. struct snd_ctl_elem_value *ucontrol)
  502. {
  503. struct tas *tas = snd_kcontrol_chip(kcontrol);
  504. mutex_lock(&tas->mtx);
  505. if (tas->treble == ucontrol->value.integer.value[0]) {
  506. mutex_unlock(&tas->mtx);
  507. return 0;
  508. }
  509. tas->treble = ucontrol->value.integer.value[0];
  510. if (tas->hw_enabled)
  511. tas_set_treble(tas);
  512. mutex_unlock(&tas->mtx);
  513. return 1;
  514. }
  515. static struct snd_kcontrol_new treble_control = {
  516. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  517. .name = "Treble",
  518. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  519. .info = tas_snd_treble_info,
  520. .get = tas_snd_treble_get,
  521. .put = tas_snd_treble_put,
  522. };
  523. static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
  524. struct snd_ctl_elem_info *uinfo)
  525. {
  526. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  527. uinfo->count = 1;
  528. uinfo->value.integer.min = TAS3004_BASS_MIN;
  529. uinfo->value.integer.max = TAS3004_BASS_MAX;
  530. return 0;
  531. }
  532. static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
  533. struct snd_ctl_elem_value *ucontrol)
  534. {
  535. struct tas *tas = snd_kcontrol_chip(kcontrol);
  536. mutex_lock(&tas->mtx);
  537. ucontrol->value.integer.value[0] = tas->bass;
  538. mutex_unlock(&tas->mtx);
  539. return 0;
  540. }
  541. static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
  542. struct snd_ctl_elem_value *ucontrol)
  543. {
  544. struct tas *tas = snd_kcontrol_chip(kcontrol);
  545. mutex_lock(&tas->mtx);
  546. if (tas->bass == ucontrol->value.integer.value[0]) {
  547. mutex_unlock(&tas->mtx);
  548. return 0;
  549. }
  550. tas->bass = ucontrol->value.integer.value[0];
  551. if (tas->hw_enabled)
  552. tas_set_bass(tas);
  553. mutex_unlock(&tas->mtx);
  554. return 1;
  555. }
  556. static struct snd_kcontrol_new bass_control = {
  557. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  558. .name = "Bass",
  559. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  560. .info = tas_snd_bass_info,
  561. .get = tas_snd_bass_get,
  562. .put = tas_snd_bass_put,
  563. };
  564. static struct transfer_info tas_transfers[] = {
  565. {
  566. /* input */
  567. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  568. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  569. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  570. .transfer_in = 1,
  571. },
  572. {
  573. /* output */
  574. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  575. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  576. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  577. .transfer_in = 0,
  578. },
  579. {}
  580. };
  581. static int tas_usable(struct codec_info_item *cii,
  582. struct transfer_info *ti,
  583. struct transfer_info *out)
  584. {
  585. return 1;
  586. }
  587. static int tas_reset_init(struct tas *tas)
  588. {
  589. u8 tmp;
  590. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  591. msleep(5);
  592. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  593. msleep(5);
  594. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  595. msleep(20);
  596. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  597. msleep(10);
  598. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  599. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  600. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  601. goto outerr;
  602. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  603. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  604. goto outerr;
  605. tmp = 0;
  606. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  607. goto outerr;
  608. tas3004_set_drc(tas);
  609. /* Set treble & bass to 0dB */
  610. tas->treble = TAS3004_TREBLE_ZERO;
  611. tas->bass = TAS3004_BASS_ZERO;
  612. tas_set_treble(tas);
  613. tas_set_bass(tas);
  614. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  615. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  616. goto outerr;
  617. return 0;
  618. outerr:
  619. return -ENODEV;
  620. }
  621. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  622. {
  623. struct tas *tas = cii->codec_data;
  624. switch(clock) {
  625. case CLOCK_SWITCH_PREPARE_SLAVE:
  626. /* Clocks are going away, mute mute mute */
  627. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  628. tas->hw_enabled = 0;
  629. break;
  630. case CLOCK_SWITCH_SLAVE:
  631. /* Clocks are back, re-init the codec */
  632. mutex_lock(&tas->mtx);
  633. tas_reset_init(tas);
  634. tas_set_volume(tas);
  635. tas_set_mixer(tas);
  636. tas->hw_enabled = 1;
  637. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  638. mutex_unlock(&tas->mtx);
  639. break;
  640. default:
  641. /* doesn't happen as of now */
  642. return -EINVAL;
  643. }
  644. return 0;
  645. }
  646. /* we are controlled via i2c and assume that is always up
  647. * If that wasn't the case, we'd have to suspend once
  648. * our i2c device is suspended, and then take note of that! */
  649. static int tas_suspend(struct tas *tas)
  650. {
  651. mutex_lock(&tas->mtx);
  652. tas->hw_enabled = 0;
  653. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  654. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  655. mutex_unlock(&tas->mtx);
  656. return 0;
  657. }
  658. static int tas_resume(struct tas *tas)
  659. {
  660. /* reset codec */
  661. mutex_lock(&tas->mtx);
  662. tas_reset_init(tas);
  663. tas_set_volume(tas);
  664. tas_set_mixer(tas);
  665. tas->hw_enabled = 1;
  666. mutex_unlock(&tas->mtx);
  667. return 0;
  668. }
  669. #ifdef CONFIG_PM
  670. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  671. {
  672. return tas_suspend(cii->codec_data);
  673. }
  674. static int _tas_resume(struct codec_info_item *cii)
  675. {
  676. return tas_resume(cii->codec_data);
  677. }
  678. #endif
  679. static struct codec_info tas_codec_info = {
  680. .transfers = tas_transfers,
  681. /* in theory, we can drive it at 512 too...
  682. * but so far the framework doesn't allow
  683. * for that and I don't see much point in it. */
  684. .sysclock_factor = 256,
  685. /* same here, could be 32 for just one 16 bit format */
  686. .bus_factor = 64,
  687. .owner = THIS_MODULE,
  688. .usable = tas_usable,
  689. .switch_clock = tas_switch_clock,
  690. #ifdef CONFIG_PM
  691. .suspend = _tas_suspend,
  692. .resume = _tas_resume,
  693. #endif
  694. };
  695. static int tas_init_codec(struct aoa_codec *codec)
  696. {
  697. struct tas *tas = codec_to_tas(codec);
  698. int err;
  699. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  700. printk(KERN_ERR PFX "gpios not assigned!!\n");
  701. return -EINVAL;
  702. }
  703. mutex_lock(&tas->mtx);
  704. if (tas_reset_init(tas)) {
  705. printk(KERN_ERR PFX "tas failed to initialise\n");
  706. mutex_unlock(&tas->mtx);
  707. return -ENXIO;
  708. }
  709. tas->hw_enabled = 1;
  710. mutex_unlock(&tas->mtx);
  711. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  712. aoa_get_card(),
  713. &tas_codec_info, tas)) {
  714. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  715. return -ENODEV;
  716. }
  717. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
  718. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  719. return -ENODEV;
  720. }
  721. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  722. if (err)
  723. goto error;
  724. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  725. if (err)
  726. goto error;
  727. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  728. if (err)
  729. goto error;
  730. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  731. if (err)
  732. goto error;
  733. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  734. if (err)
  735. goto error;
  736. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
  737. if (err)
  738. goto error;
  739. err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
  740. if (err)
  741. goto error;
  742. err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
  743. if (err)
  744. goto error;
  745. err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
  746. if (err)
  747. goto error;
  748. return 0;
  749. error:
  750. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  751. snd_device_free(aoa_get_card(), tas);
  752. return err;
  753. }
  754. static void tas_exit_codec(struct aoa_codec *codec)
  755. {
  756. struct tas *tas = codec_to_tas(codec);
  757. if (!tas->codec.soundbus_dev)
  758. return;
  759. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  760. }
  761. static struct i2c_driver tas_driver;
  762. static int tas_create(struct i2c_adapter *adapter,
  763. struct device_node *node,
  764. int addr)
  765. {
  766. struct tas *tas;
  767. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  768. if (!tas)
  769. return -ENOMEM;
  770. mutex_init(&tas->mtx);
  771. tas->i2c.driver = &tas_driver;
  772. tas->i2c.adapter = adapter;
  773. tas->i2c.addr = addr;
  774. /* seems that half is a saner default */
  775. tas->drc_range = TAS3004_DRC_MAX / 2;
  776. strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE);
  777. if (i2c_attach_client(&tas->i2c)) {
  778. printk(KERN_ERR PFX "failed to attach to i2c\n");
  779. goto fail;
  780. }
  781. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
  782. tas->codec.owner = THIS_MODULE;
  783. tas->codec.init = tas_init_codec;
  784. tas->codec.exit = tas_exit_codec;
  785. tas->codec.node = of_node_get(node);
  786. if (aoa_codec_register(&tas->codec)) {
  787. goto detach;
  788. }
  789. printk(KERN_DEBUG
  790. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  791. addr, node->full_name);
  792. return 0;
  793. detach:
  794. i2c_detach_client(&tas->i2c);
  795. fail:
  796. mutex_destroy(&tas->mtx);
  797. kfree(tas);
  798. return -EINVAL;
  799. }
  800. static int tas_i2c_attach(struct i2c_adapter *adapter)
  801. {
  802. struct device_node *busnode, *dev = NULL;
  803. struct pmac_i2c_bus *bus;
  804. bus = pmac_i2c_adapter_to_bus(adapter);
  805. if (bus == NULL)
  806. return -ENODEV;
  807. busnode = pmac_i2c_get_bus_node(bus);
  808. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  809. if (of_device_is_compatible(dev, "tas3004")) {
  810. const u32 *addr;
  811. printk(KERN_DEBUG PFX "found tas3004\n");
  812. addr = of_get_property(dev, "reg", NULL);
  813. if (!addr)
  814. continue;
  815. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  816. }
  817. /* older machines have no 'codec' node with a 'compatible'
  818. * property that says 'tas3004', they just have a 'deq'
  819. * node without any such property... */
  820. if (strcmp(dev->name, "deq") == 0) {
  821. const u32 *_addr;
  822. u32 addr;
  823. printk(KERN_DEBUG PFX "found 'deq' node\n");
  824. _addr = of_get_property(dev, "i2c-address", NULL);
  825. if (!_addr)
  826. continue;
  827. addr = ((*_addr) >> 1) & 0x7f;
  828. /* now, if the address doesn't match any of the two
  829. * that a tas3004 can have, we cannot handle this.
  830. * I doubt it ever happens but hey. */
  831. if (addr != 0x34 && addr != 0x35)
  832. continue;
  833. return tas_create(adapter, dev, addr);
  834. }
  835. }
  836. return -ENODEV;
  837. }
  838. static int tas_i2c_detach(struct i2c_client *client)
  839. {
  840. struct tas *tas = container_of(client, struct tas, i2c);
  841. int err;
  842. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  843. if ((err = i2c_detach_client(client)))
  844. return err;
  845. aoa_codec_unregister(&tas->codec);
  846. of_node_put(tas->codec.node);
  847. /* power down codec chip */
  848. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  849. mutex_destroy(&tas->mtx);
  850. kfree(tas);
  851. return 0;
  852. }
  853. static struct i2c_driver tas_driver = {
  854. .driver = {
  855. .name = "aoa_codec_tas",
  856. .owner = THIS_MODULE,
  857. },
  858. .attach_adapter = tas_i2c_attach,
  859. .detach_client = tas_i2c_detach,
  860. };
  861. static int __init tas_init(void)
  862. {
  863. return i2c_add_driver(&tas_driver);
  864. }
  865. static void __exit tas_exit(void)
  866. {
  867. i2c_del_driver(&tas_driver);
  868. }
  869. module_init(tas_init);
  870. module_exit(tas_exit);