p1022_ds.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /**
  2. * Freescale P1022DS ALSA SoC Machine driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2010 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/of_device.h>
  15. #include <linux/slab.h>
  16. #include <sound/soc.h>
  17. #include <asm/fsl_guts.h>
  18. #include "fsl_dma.h"
  19. #include "fsl_ssi.h"
  20. /* P1022-specific PMUXCR and DMUXCR bit definitions */
  21. #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000
  22. #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000
  23. #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000
  24. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00
  25. #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000
  26. #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */
  27. #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */
  28. /*
  29. * Set the DMACR register in the GUTS
  30. *
  31. * The DMACR register determines the source of initiated transfers for each
  32. * channel on each DMA controller. Rather than have a bunch of repetitive
  33. * macros for the bit patterns, we just have a function that calculates
  34. * them.
  35. *
  36. * guts: Pointer to GUTS structure
  37. * co: The DMA controller (0 or 1)
  38. * ch: The channel on the DMA controller (0, 1, 2, or 3)
  39. * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx)
  40. */
  41. static inline void guts_set_dmuxcr(struct ccsr_guts_85xx __iomem *guts,
  42. unsigned int co, unsigned int ch, unsigned int device)
  43. {
  44. unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch));
  45. clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift);
  46. }
  47. /* There's only one global utilities register */
  48. static phys_addr_t guts_phys;
  49. #define DAI_NAME_SIZE 32
  50. /**
  51. * machine_data: machine-specific ASoC device data
  52. *
  53. * This structure contains data for a single sound platform device on an
  54. * P1022 DS. Some of the data is taken from the device tree.
  55. */
  56. struct machine_data {
  57. struct snd_soc_dai_link dai[2];
  58. struct snd_soc_card card;
  59. unsigned int dai_format;
  60. unsigned int codec_clk_direction;
  61. unsigned int cpu_clk_direction;
  62. unsigned int clk_frequency;
  63. unsigned int ssi_id; /* 0 = SSI1, 1 = SSI2, etc */
  64. unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */
  65. unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/
  66. char codec_name[DAI_NAME_SIZE];
  67. char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */
  68. };
  69. /**
  70. * p1022_ds_machine_probe: initialize the board
  71. *
  72. * This function is used to initialize the board-specific hardware.
  73. *
  74. * Here we program the DMACR and PMUXCR registers.
  75. */
  76. static int p1022_ds_machine_probe(struct snd_soc_card *card)
  77. {
  78. struct machine_data *mdata =
  79. container_of(card, struct machine_data, card);
  80. struct ccsr_guts_85xx __iomem *guts;
  81. guts = ioremap(guts_phys, sizeof(struct ccsr_guts_85xx));
  82. if (!guts) {
  83. dev_err(card->dev, "could not map global utilities\n");
  84. return -ENOMEM;
  85. }
  86. /* Enable SSI Tx signal */
  87. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK,
  88. CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI);
  89. /* Enable SSI Rx signal */
  90. clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK,
  91. CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI);
  92. /* Enable DMA Channel for SSI */
  93. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0],
  94. CCSR_GUTS_DMUXCR_SSI);
  95. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1],
  96. CCSR_GUTS_DMUXCR_SSI);
  97. iounmap(guts);
  98. return 0;
  99. }
  100. /**
  101. * p1022_ds_startup: program the board with various hardware parameters
  102. *
  103. * This function takes board-specific information, like clock frequencies
  104. * and serial data formats, and passes that information to the codec and
  105. * transport drivers.
  106. */
  107. static int p1022_ds_startup(struct snd_pcm_substream *substream)
  108. {
  109. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  110. struct machine_data *mdata =
  111. container_of(rtd->card, struct machine_data, card);
  112. struct device *dev = rtd->card->dev;
  113. int ret = 0;
  114. /* Tell the codec driver what the serial protocol is. */
  115. ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format);
  116. if (ret < 0) {
  117. dev_err(dev, "could not set codec driver audio format\n");
  118. return ret;
  119. }
  120. /*
  121. * Tell the codec driver what the MCLK frequency is, and whether it's
  122. * a slave or master.
  123. */
  124. ret = snd_soc_dai_set_sysclk(rtd->codec_dai, 0, mdata->clk_frequency,
  125. mdata->codec_clk_direction);
  126. if (ret < 0) {
  127. dev_err(dev, "could not set codec driver clock params\n");
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * p1022_ds_machine_remove: Remove the sound device
  134. *
  135. * This function is called to remove the sound device for one SSI. We
  136. * de-program the DMACR and PMUXCR register.
  137. */
  138. static int p1022_ds_machine_remove(struct snd_soc_card *card)
  139. {
  140. struct machine_data *mdata =
  141. container_of(card, struct machine_data, card);
  142. struct ccsr_guts_85xx __iomem *guts;
  143. guts = ioremap(guts_phys, sizeof(struct ccsr_guts_85xx));
  144. if (!guts) {
  145. dev_err(card->dev, "could not map global utilities\n");
  146. return -ENOMEM;
  147. }
  148. /* Restore the signal routing */
  149. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK);
  150. clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK);
  151. guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0);
  152. guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0);
  153. iounmap(guts);
  154. return 0;
  155. }
  156. /**
  157. * p1022_ds_ops: ASoC machine driver operations
  158. */
  159. static struct snd_soc_ops p1022_ds_ops = {
  160. .startup = p1022_ds_startup,
  161. };
  162. /**
  163. * get_node_by_phandle_name - get a node by its phandle name
  164. *
  165. * This function takes a node, the name of a property in that node, and a
  166. * compatible string. Assuming the property is a phandle to another node,
  167. * it returns that node, (optionally) if that node is compatible.
  168. *
  169. * If the property is not a phandle, or the node it points to is not compatible
  170. * with the specific string, then NULL is returned.
  171. */
  172. static struct device_node *get_node_by_phandle_name(struct device_node *np,
  173. const char *name, const char *compatible)
  174. {
  175. np = of_parse_phandle(np, name, 0);
  176. if (!np)
  177. return NULL;
  178. if (!of_device_is_compatible(np, compatible)) {
  179. of_node_put(np);
  180. return NULL;
  181. }
  182. return np;
  183. }
  184. /**
  185. * get_parent_cell_index -- return the cell-index of the parent of a node
  186. *
  187. * Return the value of the cell-index property of the parent of the given
  188. * node. This is used for DMA channel nodes that need to know the DMA ID
  189. * of the controller they are on.
  190. */
  191. static int get_parent_cell_index(struct device_node *np)
  192. {
  193. struct device_node *parent = of_get_parent(np);
  194. const u32 *iprop;
  195. int ret = -1;
  196. if (!parent)
  197. return -1;
  198. iprop = of_get_property(parent, "cell-index", NULL);
  199. if (iprop)
  200. ret = *iprop;
  201. of_node_put(parent);
  202. return ret;
  203. }
  204. /**
  205. * codec_node_dev_name - determine the dev_name for a codec node
  206. *
  207. * This function determines the dev_name for an I2C node. This is the name
  208. * that would be returned by dev_name() if this device_node were part of a
  209. * 'struct device' It's ugly and hackish, but it works.
  210. *
  211. * The dev_name for such devices include the bus number and I2C address. For
  212. * example, "cs4270-codec.0-004f".
  213. */
  214. static int codec_node_dev_name(struct device_node *np, char *buf, size_t len)
  215. {
  216. const u32 *iprop;
  217. int bus, addr;
  218. char temp[DAI_NAME_SIZE];
  219. of_modalias_node(np, temp, DAI_NAME_SIZE);
  220. iprop = of_get_property(np, "reg", NULL);
  221. if (!iprop)
  222. return -EINVAL;
  223. addr = *iprop;
  224. bus = get_parent_cell_index(np);
  225. if (bus < 0)
  226. return bus;
  227. snprintf(buf, len, "%s-codec.%u-%04x", temp, bus, addr);
  228. return 0;
  229. }
  230. static int get_dma_channel(struct device_node *ssi_np,
  231. const char *compatible,
  232. struct snd_soc_dai_link *dai,
  233. unsigned int *dma_channel_id,
  234. unsigned int *dma_id)
  235. {
  236. struct resource res;
  237. struct device_node *dma_channel_np;
  238. const u32 *iprop;
  239. int ret;
  240. dma_channel_np = get_node_by_phandle_name(ssi_np, compatible,
  241. "fsl,ssi-dma-channel");
  242. if (!dma_channel_np)
  243. return -EINVAL;
  244. /* Determine the dev_name for the device_node. This code mimics the
  245. * behavior of of_device_make_bus_id(). We need this because ASoC uses
  246. * the dev_name() of the device to match the platform (DMA) device with
  247. * the CPU (SSI) device. It's all ugly and hackish, but it works (for
  248. * now).
  249. *
  250. * dai->platform name should already point to an allocated buffer.
  251. */
  252. ret = of_address_to_resource(dma_channel_np, 0, &res);
  253. if (ret)
  254. return ret;
  255. snprintf((char *)dai->platform_name, DAI_NAME_SIZE, "%llx.%s",
  256. (unsigned long long) res.start, dma_channel_np->name);
  257. iprop = of_get_property(dma_channel_np, "cell-index", NULL);
  258. if (!iprop) {
  259. of_node_put(dma_channel_np);
  260. return -EINVAL;
  261. }
  262. *dma_channel_id = *iprop;
  263. *dma_id = get_parent_cell_index(dma_channel_np);
  264. of_node_put(dma_channel_np);
  265. return 0;
  266. }
  267. /**
  268. * p1022_ds_probe: platform probe function for the machine driver
  269. *
  270. * Although this is a machine driver, the SSI node is the "master" node with
  271. * respect to audio hardware connections. Therefore, we create a new ASoC
  272. * device for each new SSI node that has a codec attached.
  273. */
  274. static int p1022_ds_probe(struct platform_device *pdev)
  275. {
  276. struct device *dev = pdev->dev.parent;
  277. /* ssi_pdev is the platform device for the SSI node that probed us */
  278. struct platform_device *ssi_pdev =
  279. container_of(dev, struct platform_device, dev);
  280. struct device_node *np = ssi_pdev->dev.of_node;
  281. struct device_node *codec_np = NULL;
  282. struct platform_device *sound_device = NULL;
  283. struct machine_data *mdata;
  284. int ret = -ENODEV;
  285. const char *sprop;
  286. const u32 *iprop;
  287. /* Find the codec node for this SSI. */
  288. codec_np = of_parse_phandle(np, "codec-handle", 0);
  289. if (!codec_np) {
  290. dev_err(dev, "could not find codec node\n");
  291. return -EINVAL;
  292. }
  293. mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL);
  294. if (!mdata) {
  295. ret = -ENOMEM;
  296. goto error_put;
  297. }
  298. mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev);
  299. mdata->dai[0].ops = &p1022_ds_ops;
  300. /* Determine the codec name, it will be used as the codec DAI name */
  301. ret = codec_node_dev_name(codec_np, mdata->codec_name, DAI_NAME_SIZE);
  302. if (ret) {
  303. dev_err(&pdev->dev, "invalid codec node %s\n",
  304. codec_np->full_name);
  305. ret = -EINVAL;
  306. goto error;
  307. }
  308. mdata->dai[0].codec_name = mdata->codec_name;
  309. /* We register two DAIs per SSI, one for playback and the other for
  310. * capture. We support codecs that have separate DAIs for both playback
  311. * and capture.
  312. */
  313. memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link));
  314. /* The DAI names from the codec (snd_soc_dai_driver.name) */
  315. mdata->dai[0].codec_dai_name = "wm8776-hifi-playback";
  316. mdata->dai[1].codec_dai_name = "wm8776-hifi-capture";
  317. /* Get the device ID */
  318. iprop = of_get_property(np, "cell-index", NULL);
  319. if (!iprop) {
  320. dev_err(&pdev->dev, "cell-index property not found\n");
  321. ret = -EINVAL;
  322. goto error;
  323. }
  324. mdata->ssi_id = *iprop;
  325. /* Get the serial format and clock direction. */
  326. sprop = of_get_property(np, "fsl,mode", NULL);
  327. if (!sprop) {
  328. dev_err(&pdev->dev, "fsl,mode property not found\n");
  329. ret = -EINVAL;
  330. goto error;
  331. }
  332. if (strcasecmp(sprop, "i2s-slave") == 0) {
  333. mdata->dai_format = SND_SOC_DAIFMT_I2S;
  334. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  335. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  336. /* In i2s-slave mode, the codec has its own clock source, so we
  337. * need to get the frequency from the device tree and pass it to
  338. * the codec driver.
  339. */
  340. iprop = of_get_property(codec_np, "clock-frequency", NULL);
  341. if (!iprop || !*iprop) {
  342. dev_err(&pdev->dev, "codec bus-frequency "
  343. "property is missing or invalid\n");
  344. ret = -EINVAL;
  345. goto error;
  346. }
  347. mdata->clk_frequency = *iprop;
  348. } else if (strcasecmp(sprop, "i2s-master") == 0) {
  349. mdata->dai_format = SND_SOC_DAIFMT_I2S;
  350. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  351. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  352. } else if (strcasecmp(sprop, "lj-slave") == 0) {
  353. mdata->dai_format = SND_SOC_DAIFMT_LEFT_J;
  354. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  355. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  356. } else if (strcasecmp(sprop, "lj-master") == 0) {
  357. mdata->dai_format = SND_SOC_DAIFMT_LEFT_J;
  358. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  359. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  360. } else if (strcasecmp(sprop, "rj-slave") == 0) {
  361. mdata->dai_format = SND_SOC_DAIFMT_RIGHT_J;
  362. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  363. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  364. } else if (strcasecmp(sprop, "rj-master") == 0) {
  365. mdata->dai_format = SND_SOC_DAIFMT_RIGHT_J;
  366. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  367. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  368. } else if (strcasecmp(sprop, "ac97-slave") == 0) {
  369. mdata->dai_format = SND_SOC_DAIFMT_AC97;
  370. mdata->codec_clk_direction = SND_SOC_CLOCK_OUT;
  371. mdata->cpu_clk_direction = SND_SOC_CLOCK_IN;
  372. } else if (strcasecmp(sprop, "ac97-master") == 0) {
  373. mdata->dai_format = SND_SOC_DAIFMT_AC97;
  374. mdata->codec_clk_direction = SND_SOC_CLOCK_IN;
  375. mdata->cpu_clk_direction = SND_SOC_CLOCK_OUT;
  376. } else {
  377. dev_err(&pdev->dev,
  378. "unrecognized fsl,mode property '%s'\n", sprop);
  379. ret = -EINVAL;
  380. goto error;
  381. }
  382. if (!mdata->clk_frequency) {
  383. dev_err(&pdev->dev, "unknown clock frequency\n");
  384. ret = -EINVAL;
  385. goto error;
  386. }
  387. /* Find the playback DMA channel to use. */
  388. mdata->dai[0].platform_name = mdata->platform_name[0];
  389. ret = get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0],
  390. &mdata->dma_channel_id[0],
  391. &mdata->dma_id[0]);
  392. if (ret) {
  393. dev_err(&pdev->dev, "missing/invalid playback DMA phandle\n");
  394. goto error;
  395. }
  396. /* Find the capture DMA channel to use. */
  397. mdata->dai[1].platform_name = mdata->platform_name[1];
  398. ret = get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1],
  399. &mdata->dma_channel_id[1],
  400. &mdata->dma_id[1]);
  401. if (ret) {
  402. dev_err(&pdev->dev, "missing/invalid capture DMA phandle\n");
  403. goto error;
  404. }
  405. /* Initialize our DAI data structure. */
  406. mdata->dai[0].stream_name = "playback";
  407. mdata->dai[1].stream_name = "capture";
  408. mdata->dai[0].name = mdata->dai[0].stream_name;
  409. mdata->dai[1].name = mdata->dai[1].stream_name;
  410. mdata->card.probe = p1022_ds_machine_probe;
  411. mdata->card.remove = p1022_ds_machine_remove;
  412. mdata->card.name = pdev->name; /* The platform driver name */
  413. mdata->card.num_links = 2;
  414. mdata->card.dai_link = mdata->dai;
  415. /* Allocate a new audio platform device structure */
  416. sound_device = platform_device_alloc("soc-audio", -1);
  417. if (!sound_device) {
  418. dev_err(&pdev->dev, "platform device alloc failed\n");
  419. ret = -ENOMEM;
  420. goto error;
  421. }
  422. /* Associate the card data with the sound device */
  423. platform_set_drvdata(sound_device, &mdata->card);
  424. /* Register with ASoC */
  425. ret = platform_device_add(sound_device);
  426. if (ret) {
  427. dev_err(&pdev->dev, "platform device add failed\n");
  428. goto error;
  429. }
  430. dev_set_drvdata(&pdev->dev, sound_device);
  431. of_node_put(codec_np);
  432. return 0;
  433. error:
  434. if (sound_device)
  435. platform_device_unregister(sound_device);
  436. kfree(mdata);
  437. error_put:
  438. of_node_put(codec_np);
  439. return ret;
  440. }
  441. /**
  442. * p1022_ds_remove: remove the platform device
  443. *
  444. * This function is called when the platform device is removed.
  445. */
  446. static int __devexit p1022_ds_remove(struct platform_device *pdev)
  447. {
  448. struct platform_device *sound_device = dev_get_drvdata(&pdev->dev);
  449. struct snd_soc_card *card = platform_get_drvdata(sound_device);
  450. struct machine_data *mdata =
  451. container_of(card, struct machine_data, card);
  452. platform_device_unregister(sound_device);
  453. kfree(mdata);
  454. sound_device->dev.platform_data = NULL;
  455. dev_set_drvdata(&pdev->dev, NULL);
  456. return 0;
  457. }
  458. static struct platform_driver p1022_ds_driver = {
  459. .probe = p1022_ds_probe,
  460. .remove = __devexit_p(p1022_ds_remove),
  461. .driver = {
  462. /* The name must match the 'model' property in the device tree,
  463. * in lowercase letters, but only the part after that last
  464. * comma. This is because some model properties have a "fsl,"
  465. * prefix.
  466. */
  467. .name = "snd-soc-p1022",
  468. .owner = THIS_MODULE,
  469. },
  470. };
  471. /**
  472. * p1022_ds_init: machine driver initialization.
  473. *
  474. * This function is called when this module is loaded.
  475. */
  476. static int __init p1022_ds_init(void)
  477. {
  478. struct device_node *guts_np;
  479. struct resource res;
  480. pr_info("Freescale P1022 DS ALSA SoC machine driver\n");
  481. /* Get the physical address of the global utilities registers */
  482. guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts");
  483. if (of_address_to_resource(guts_np, 0, &res)) {
  484. pr_err("p1022-ds: missing/invalid global utilities node\n");
  485. return -EINVAL;
  486. }
  487. guts_phys = res.start;
  488. of_node_put(guts_np);
  489. return platform_driver_register(&p1022_ds_driver);
  490. }
  491. /**
  492. * p1022_ds_exit: machine driver exit
  493. *
  494. * This function is called when this driver is unloaded.
  495. */
  496. static void __exit p1022_ds_exit(void)
  497. {
  498. platform_driver_unregister(&p1022_ds_driver);
  499. }
  500. module_init(p1022_ds_init);
  501. module_exit(p1022_ds_exit);
  502. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  503. MODULE_DESCRIPTION("Freescale P1022 DS ALSA SoC machine driver");
  504. MODULE_LICENSE("GPL v2");