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