snd-aoa-codec-tas.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. 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 "../aoa.h"
  75. #include "../soundbus/soundbus.h"
  76. #define PFX "snd-aoa-codec-tas: "
  77. struct tas {
  78. struct aoa_codec codec;
  79. struct i2c_client i2c;
  80. u32 mute_l:1, mute_r:1 ,
  81. controls_created:1 ,
  82. drc_enabled:1,
  83. hw_enabled:1;
  84. u8 cached_volume_l, cached_volume_r;
  85. u8 mixer_l[3], mixer_r[3];
  86. u8 acr;
  87. int drc_range;
  88. };
  89. static int tas_reset_init(struct tas *tas);
  90. static struct tas *codec_to_tas(struct aoa_codec *codec)
  91. {
  92. return container_of(codec, struct tas, codec);
  93. }
  94. static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
  95. {
  96. if (len == 1)
  97. return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
  98. else
  99. return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
  100. }
  101. static void tas3004_set_drc(struct tas *tas)
  102. {
  103. unsigned char val[6];
  104. if (tas->drc_enabled)
  105. val[0] = 0x50; /* 3:1 above threshold */
  106. else
  107. val[0] = 0x51; /* disabled */
  108. val[1] = 0x02; /* 1:1 below threshold */
  109. if (tas->drc_range > 0xef)
  110. val[2] = 0xef;
  111. else if (tas->drc_range < 0)
  112. val[2] = 0x00;
  113. else
  114. val[2] = tas->drc_range;
  115. val[3] = 0xb0;
  116. val[4] = 0x60;
  117. val[5] = 0xa0;
  118. tas_write_reg(tas, TAS_REG_DRC, 6, val);
  119. }
  120. static void tas_set_volume(struct tas *tas)
  121. {
  122. u8 block[6];
  123. int tmp;
  124. u8 left, right;
  125. left = tas->cached_volume_l;
  126. right = tas->cached_volume_r;
  127. if (left > 177) left = 177;
  128. if (right > 177) right = 177;
  129. if (tas->mute_l) left = 0;
  130. if (tas->mute_r) right = 0;
  131. /* analysing the volume and mixer tables shows
  132. * that they are similar enough when we shift
  133. * the mixer table down by 4 bits. The error
  134. * is miniscule, in just one item the error
  135. * is 1, at a value of 0x07f17b (mixer table
  136. * value is 0x07f17a) */
  137. tmp = tas_gaintable[left];
  138. block[0] = tmp>>20;
  139. block[1] = tmp>>12;
  140. block[2] = tmp>>4;
  141. tmp = tas_gaintable[right];
  142. block[3] = tmp>>20;
  143. block[4] = tmp>>12;
  144. block[5] = tmp>>4;
  145. tas_write_reg(tas, TAS_REG_VOL, 6, block);
  146. }
  147. static void tas_set_mixer(struct tas *tas)
  148. {
  149. u8 block[9];
  150. int tmp, i;
  151. u8 val;
  152. for (i=0;i<3;i++) {
  153. val = tas->mixer_l[i];
  154. if (val > 177) val = 177;
  155. tmp = tas_gaintable[val];
  156. block[3*i+0] = tmp>>16;
  157. block[3*i+1] = tmp>>8;
  158. block[3*i+2] = tmp;
  159. }
  160. tas_write_reg(tas, TAS_REG_LMIX, 9, block);
  161. for (i=0;i<3;i++) {
  162. val = tas->mixer_r[i];
  163. if (val > 177) val = 177;
  164. tmp = tas_gaintable[val];
  165. block[3*i+0] = tmp>>16;
  166. block[3*i+1] = tmp>>8;
  167. block[3*i+2] = tmp;
  168. }
  169. tas_write_reg(tas, TAS_REG_RMIX, 9, block);
  170. }
  171. /* alsa stuff */
  172. static int tas_dev_register(struct snd_device *dev)
  173. {
  174. return 0;
  175. }
  176. static struct snd_device_ops ops = {
  177. .dev_register = tas_dev_register,
  178. };
  179. static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_info *uinfo)
  181. {
  182. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  183. uinfo->count = 2;
  184. uinfo->value.integer.min = 0;
  185. uinfo->value.integer.max = 177;
  186. return 0;
  187. }
  188. static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
  189. struct snd_ctl_elem_value *ucontrol)
  190. {
  191. struct tas *tas = snd_kcontrol_chip(kcontrol);
  192. ucontrol->value.integer.value[0] = tas->cached_volume_l;
  193. ucontrol->value.integer.value[1] = tas->cached_volume_r;
  194. return 0;
  195. }
  196. static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
  197. struct snd_ctl_elem_value *ucontrol)
  198. {
  199. struct tas *tas = snd_kcontrol_chip(kcontrol);
  200. if (tas->cached_volume_l == ucontrol->value.integer.value[0]
  201. && tas->cached_volume_r == ucontrol->value.integer.value[1])
  202. return 0;
  203. tas->cached_volume_l = ucontrol->value.integer.value[0];
  204. tas->cached_volume_r = ucontrol->value.integer.value[1];
  205. if (tas->hw_enabled)
  206. tas_set_volume(tas);
  207. return 1;
  208. }
  209. static struct snd_kcontrol_new volume_control = {
  210. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  211. .name = "Master Playback Volume",
  212. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  213. .info = tas_snd_vol_info,
  214. .get = tas_snd_vol_get,
  215. .put = tas_snd_vol_put,
  216. };
  217. static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_info *uinfo)
  219. {
  220. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  221. uinfo->count = 2;
  222. uinfo->value.integer.min = 0;
  223. uinfo->value.integer.max = 1;
  224. return 0;
  225. }
  226. static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_value *ucontrol)
  228. {
  229. struct tas *tas = snd_kcontrol_chip(kcontrol);
  230. ucontrol->value.integer.value[0] = !tas->mute_l;
  231. ucontrol->value.integer.value[1] = !tas->mute_r;
  232. return 0;
  233. }
  234. static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
  235. struct snd_ctl_elem_value *ucontrol)
  236. {
  237. struct tas *tas = snd_kcontrol_chip(kcontrol);
  238. if (tas->mute_l == !ucontrol->value.integer.value[0]
  239. && tas->mute_r == !ucontrol->value.integer.value[1])
  240. return 0;
  241. tas->mute_l = !ucontrol->value.integer.value[0];
  242. tas->mute_r = !ucontrol->value.integer.value[1];
  243. if (tas->hw_enabled)
  244. tas_set_volume(tas);
  245. return 1;
  246. }
  247. static struct snd_kcontrol_new mute_control = {
  248. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  249. .name = "Master Playback Switch",
  250. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  251. .info = tas_snd_mute_info,
  252. .get = tas_snd_mute_get,
  253. .put = tas_snd_mute_put,
  254. };
  255. static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_info *uinfo)
  257. {
  258. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  259. uinfo->count = 2;
  260. uinfo->value.integer.min = 0;
  261. uinfo->value.integer.max = 177;
  262. return 0;
  263. }
  264. static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct tas *tas = snd_kcontrol_chip(kcontrol);
  268. int idx = kcontrol->private_value;
  269. ucontrol->value.integer.value[0] = tas->mixer_l[idx];
  270. ucontrol->value.integer.value[1] = tas->mixer_r[idx];
  271. return 0;
  272. }
  273. static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
  274. struct snd_ctl_elem_value *ucontrol)
  275. {
  276. struct tas *tas = snd_kcontrol_chip(kcontrol);
  277. int idx = kcontrol->private_value;
  278. if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
  279. && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
  280. return 0;
  281. tas->mixer_l[idx] = ucontrol->value.integer.value[0];
  282. tas->mixer_r[idx] = ucontrol->value.integer.value[1];
  283. if (tas->hw_enabled)
  284. tas_set_mixer(tas);
  285. return 1;
  286. }
  287. #define MIXER_CONTROL(n,descr,idx) \
  288. static struct snd_kcontrol_new n##_control = { \
  289. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  290. .name = descr " Playback Volume", \
  291. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  292. .info = tas_snd_mixer_info, \
  293. .get = tas_snd_mixer_get, \
  294. .put = tas_snd_mixer_put, \
  295. .private_value = idx, \
  296. }
  297. MIXER_CONTROL(pcm1, "PCM", 0);
  298. MIXER_CONTROL(monitor, "Monitor", 2);
  299. static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
  300. struct snd_ctl_elem_info *uinfo)
  301. {
  302. static char *texts[] = { "Line-In", "Microphone" };
  303. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  304. uinfo->count = 1;
  305. uinfo->value.enumerated.items = 2;
  306. if (uinfo->value.enumerated.item > 1)
  307. uinfo->value.enumerated.item = 1;
  308. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  309. return 0;
  310. }
  311. static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
  312. struct snd_ctl_elem_value *ucontrol)
  313. {
  314. struct tas *tas = snd_kcontrol_chip(kcontrol);
  315. ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
  316. return 0;
  317. }
  318. static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
  319. struct snd_ctl_elem_value *ucontrol)
  320. {
  321. struct tas *tas = snd_kcontrol_chip(kcontrol);
  322. int oldacr = tas->acr;
  323. tas->acr &= ~TAS_ACR_INPUT_B;
  324. if (ucontrol->value.enumerated.item[0])
  325. tas->acr |= TAS_ACR_INPUT_B;
  326. if (oldacr == tas->acr)
  327. return 0;
  328. if (tas->hw_enabled)
  329. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  330. return 1;
  331. }
  332. static struct snd_kcontrol_new capture_source_control = {
  333. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  334. /* If we name this 'Input Source', it properly shows up in
  335. * alsamixer as a selection, * but it's shown under the
  336. * 'Playback' category.
  337. * If I name it 'Capture Source', it shows up in strange
  338. * ways (two bools of which one can be selected at a
  339. * time) but at least it's shown in the 'Capture'
  340. * category.
  341. * I was told that this was due to backward compatibility,
  342. * but I don't understand then why the mangling is *not*
  343. * done when I name it "Input Source".....
  344. */
  345. .name = "Capture Source",
  346. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  347. .info = tas_snd_capture_source_info,
  348. .get = tas_snd_capture_source_get,
  349. .put = tas_snd_capture_source_put,
  350. };
  351. static struct transfer_info tas_transfers[] = {
  352. {
  353. /* input */
  354. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  355. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  356. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  357. .transfer_in = 1,
  358. },
  359. {
  360. /* output */
  361. .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  362. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
  363. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  364. .transfer_in = 0,
  365. },
  366. {}
  367. };
  368. static int tas_usable(struct codec_info_item *cii,
  369. struct transfer_info *ti,
  370. struct transfer_info *out)
  371. {
  372. return 1;
  373. }
  374. static int tas_reset_init(struct tas *tas)
  375. {
  376. u8 tmp;
  377. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  378. msleep(5);
  379. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  380. msleep(5);
  381. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
  382. msleep(20);
  383. tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
  384. msleep(10);
  385. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  386. tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
  387. if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
  388. return -ENODEV;
  389. tas->acr |= TAS_ACR_ANALOG_PDOWN | TAS_ACR_B_MONAUREAL |
  390. TAS_ACR_B_MON_SEL_RIGHT;
  391. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  392. return -ENODEV;
  393. tmp = 0;
  394. if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
  395. return -ENODEV;
  396. tas3004_set_drc(tas);
  397. /* Set treble & bass to 0dB */
  398. tmp = 114;
  399. tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
  400. tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
  401. tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
  402. if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
  403. return -ENODEV;
  404. return 0;
  405. }
  406. static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
  407. {
  408. struct tas *tas = cii->codec_data;
  409. switch(clock) {
  410. case CLOCK_SWITCH_PREPARE_SLAVE:
  411. /* Clocks are going away, mute mute mute */
  412. tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
  413. tas->hw_enabled = 0;
  414. break;
  415. case CLOCK_SWITCH_SLAVE:
  416. /* Clocks are back, re-init the codec */
  417. tas_reset_init(tas);
  418. tas_set_volume(tas);
  419. tas_set_mixer(tas);
  420. tas->hw_enabled = 1;
  421. tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
  422. break;
  423. default:
  424. /* doesn't happen as of now */
  425. return -EINVAL;
  426. }
  427. return 0;
  428. }
  429. /* we are controlled via i2c and assume that is always up
  430. * If that wasn't the case, we'd have to suspend once
  431. * our i2c device is suspended, and then take note of that! */
  432. static int tas_suspend(struct tas *tas)
  433. {
  434. tas->hw_enabled = 0;
  435. tas->acr |= TAS_ACR_ANALOG_PDOWN;
  436. tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
  437. return 0;
  438. }
  439. static int tas_resume(struct tas *tas)
  440. {
  441. /* reset codec */
  442. tas_reset_init(tas);
  443. tas_set_volume(tas);
  444. tas_set_mixer(tas);
  445. tas->hw_enabled = 1;
  446. return 0;
  447. }
  448. #ifdef CONFIG_PM
  449. static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
  450. {
  451. return tas_suspend(cii->codec_data);
  452. }
  453. static int _tas_resume(struct codec_info_item *cii)
  454. {
  455. return tas_resume(cii->codec_data);
  456. }
  457. #endif
  458. static struct codec_info tas_codec_info = {
  459. .transfers = tas_transfers,
  460. /* in theory, we can drive it at 512 too...
  461. * but so far the framework doesn't allow
  462. * for that and I don't see much point in it. */
  463. .sysclock_factor = 256,
  464. /* same here, could be 32 for just one 16 bit format */
  465. .bus_factor = 64,
  466. .owner = THIS_MODULE,
  467. .usable = tas_usable,
  468. .switch_clock = tas_switch_clock,
  469. #ifdef CONFIG_PM
  470. .suspend = _tas_suspend,
  471. .resume = _tas_resume,
  472. #endif
  473. };
  474. static int tas_init_codec(struct aoa_codec *codec)
  475. {
  476. struct tas *tas = codec_to_tas(codec);
  477. int err;
  478. if (!tas->codec.gpio || !tas->codec.gpio->methods) {
  479. printk(KERN_ERR PFX "gpios not assigned!!\n");
  480. return -EINVAL;
  481. }
  482. if (tas_reset_init(tas)) {
  483. printk(KERN_ERR PFX "tas failed to initialise\n");
  484. return -ENXIO;
  485. }
  486. tas->hw_enabled = 1;
  487. if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
  488. aoa_get_card(),
  489. &tas_codec_info, tas)) {
  490. printk(KERN_ERR PFX "error attaching tas to soundbus\n");
  491. return -ENODEV;
  492. }
  493. if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
  494. printk(KERN_ERR PFX "failed to create tas snd device!\n");
  495. return -ENODEV;
  496. }
  497. err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
  498. if (err)
  499. goto error;
  500. err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
  501. if (err)
  502. goto error;
  503. err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
  504. if (err)
  505. goto error;
  506. err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
  507. if (err)
  508. goto error;
  509. err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
  510. if (err)
  511. goto error;
  512. return 0;
  513. error:
  514. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  515. snd_device_free(aoa_get_card(), tas);
  516. return err;
  517. }
  518. static void tas_exit_codec(struct aoa_codec *codec)
  519. {
  520. struct tas *tas = codec_to_tas(codec);
  521. if (!tas->codec.soundbus_dev)
  522. return;
  523. tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
  524. }
  525. static struct i2c_driver tas_driver;
  526. static int tas_create(struct i2c_adapter *adapter,
  527. struct device_node *node,
  528. int addr)
  529. {
  530. struct tas *tas;
  531. tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
  532. if (!tas)
  533. return -ENOMEM;
  534. tas->i2c.driver = &tas_driver;
  535. tas->i2c.adapter = adapter;
  536. tas->i2c.addr = addr;
  537. tas->drc_range = TAS3004_DRC_MAX;
  538. strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1);
  539. if (i2c_attach_client(&tas->i2c)) {
  540. printk(KERN_ERR PFX "failed to attach to i2c\n");
  541. goto fail;
  542. }
  543. strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1);
  544. tas->codec.owner = THIS_MODULE;
  545. tas->codec.init = tas_init_codec;
  546. tas->codec.exit = tas_exit_codec;
  547. tas->codec.node = of_node_get(node);
  548. if (aoa_codec_register(&tas->codec)) {
  549. goto detach;
  550. }
  551. printk(KERN_DEBUG
  552. "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
  553. addr, node->full_name);
  554. return 0;
  555. detach:
  556. i2c_detach_client(&tas->i2c);
  557. fail:
  558. kfree(tas);
  559. return -EINVAL;
  560. }
  561. static int tas_i2c_attach(struct i2c_adapter *adapter)
  562. {
  563. struct device_node *busnode, *dev = NULL;
  564. struct pmac_i2c_bus *bus;
  565. bus = pmac_i2c_adapter_to_bus(adapter);
  566. if (bus == NULL)
  567. return -ENODEV;
  568. busnode = pmac_i2c_get_bus_node(bus);
  569. while ((dev = of_get_next_child(busnode, dev)) != NULL) {
  570. if (device_is_compatible(dev, "tas3004")) {
  571. u32 *addr;
  572. printk(KERN_DEBUG PFX "found tas3004\n");
  573. addr = (u32 *) get_property(dev, "reg", NULL);
  574. if (!addr)
  575. continue;
  576. return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
  577. }
  578. /* older machines have no 'codec' node with a 'compatible'
  579. * property that says 'tas3004', they just have a 'deq'
  580. * node without any such property... */
  581. if (strcmp(dev->name, "deq") == 0) {
  582. u32 *_addr, addr;
  583. printk(KERN_DEBUG PFX "found 'deq' node\n");
  584. _addr = (u32 *) get_property(dev, "i2c-address", NULL);
  585. if (!_addr)
  586. continue;
  587. addr = ((*_addr) >> 1) & 0x7f;
  588. /* now, if the address doesn't match any of the two
  589. * that a tas3004 can have, we cannot handle this.
  590. * I doubt it ever happens but hey. */
  591. if (addr != 0x34 && addr != 0x35)
  592. continue;
  593. return tas_create(adapter, dev, addr);
  594. }
  595. }
  596. return -ENODEV;
  597. }
  598. static int tas_i2c_detach(struct i2c_client *client)
  599. {
  600. struct tas *tas = container_of(client, struct tas, i2c);
  601. int err;
  602. u8 tmp = TAS_ACR_ANALOG_PDOWN;
  603. if ((err = i2c_detach_client(client)))
  604. return err;
  605. aoa_codec_unregister(&tas->codec);
  606. of_node_put(tas->codec.node);
  607. /* power down codec chip */
  608. tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
  609. kfree(tas);
  610. return 0;
  611. }
  612. static struct i2c_driver tas_driver = {
  613. .driver = {
  614. .name = "aoa_codec_tas",
  615. .owner = THIS_MODULE,
  616. },
  617. .attach_adapter = tas_i2c_attach,
  618. .detach_client = tas_i2c_detach,
  619. };
  620. static int __init tas_init(void)
  621. {
  622. return i2c_add_driver(&tas_driver);
  623. }
  624. static void __exit tas_exit(void)
  625. {
  626. i2c_del_driver(&tas_driver);
  627. }
  628. module_init(tas_init);
  629. module_exit(tas_exit);