ti-ssp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Sequencer Serial Port (SSP) driver for Texas Instruments' SoCs
  3. *
  4. * Copyright (C) 2010 Texas Instruments Inc
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/wait.h>
  27. #include <linux/clk.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/device.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/delay.h>
  33. #include <linux/io.h>
  34. #include <linux/mfd/core.h>
  35. #include <linux/mfd/ti_ssp.h>
  36. /* Register Offsets */
  37. #define REG_REV 0x00
  38. #define REG_IOSEL_1 0x04
  39. #define REG_IOSEL_2 0x08
  40. #define REG_PREDIV 0x0c
  41. #define REG_INTR_ST 0x10
  42. #define REG_INTR_EN 0x14
  43. #define REG_TEST_CTRL 0x18
  44. /* Per port registers */
  45. #define PORT_CFG_2 0x00
  46. #define PORT_ADDR 0x04
  47. #define PORT_DATA 0x08
  48. #define PORT_CFG_1 0x0c
  49. #define PORT_STATE 0x10
  50. #define SSP_PORT_CONFIG_MASK (SSP_EARLY_DIN | SSP_DELAY_DOUT)
  51. #define SSP_PORT_CLKRATE_MASK 0x0f
  52. #define SSP_SEQRAM_WR_EN BIT(4)
  53. #define SSP_SEQRAM_RD_EN BIT(5)
  54. #define SSP_START BIT(15)
  55. #define SSP_BUSY BIT(10)
  56. #define SSP_PORT_ASL BIT(7)
  57. #define SSP_PORT_CFO1 BIT(6)
  58. #define SSP_PORT_SEQRAM_SIZE 32
  59. static const int ssp_port_base[] = {0x040, 0x080};
  60. static const int ssp_port_seqram[] = {0x100, 0x180};
  61. struct ti_ssp {
  62. struct resource *res;
  63. struct device *dev;
  64. void __iomem *regs;
  65. spinlock_t lock;
  66. struct clk *clk;
  67. int irq;
  68. wait_queue_head_t wqh;
  69. /*
  70. * Some of the iosel2 register bits always read-back as 0, we need to
  71. * remember these values so that we don't clobber previously set
  72. * values.
  73. */
  74. u32 iosel2;
  75. };
  76. static inline struct ti_ssp *dev_to_ssp(struct device *dev)
  77. {
  78. return dev_get_drvdata(dev->parent);
  79. }
  80. static inline int dev_to_port(struct device *dev)
  81. {
  82. return to_platform_device(dev)->id;
  83. }
  84. /* Register Access Helpers, rmw() functions need to run locked */
  85. static inline u32 ssp_read(struct ti_ssp *ssp, int reg)
  86. {
  87. return __raw_readl(ssp->regs + reg);
  88. }
  89. static inline void ssp_write(struct ti_ssp *ssp, int reg, u32 val)
  90. {
  91. __raw_writel(val, ssp->regs + reg);
  92. }
  93. static inline void ssp_rmw(struct ti_ssp *ssp, int reg, u32 mask, u32 bits)
  94. {
  95. ssp_write(ssp, reg, (ssp_read(ssp, reg) & ~mask) | bits);
  96. }
  97. static inline u32 ssp_port_read(struct ti_ssp *ssp, int port, int reg)
  98. {
  99. return ssp_read(ssp, ssp_port_base[port] + reg);
  100. }
  101. static inline void ssp_port_write(struct ti_ssp *ssp, int port, int reg,
  102. u32 val)
  103. {
  104. ssp_write(ssp, ssp_port_base[port] + reg, val);
  105. }
  106. static inline void ssp_port_rmw(struct ti_ssp *ssp, int port, int reg,
  107. u32 mask, u32 bits)
  108. {
  109. ssp_rmw(ssp, ssp_port_base[port] + reg, mask, bits);
  110. }
  111. static inline void ssp_port_clr_bits(struct ti_ssp *ssp, int port, int reg,
  112. u32 bits)
  113. {
  114. ssp_port_rmw(ssp, port, reg, bits, 0);
  115. }
  116. static inline void ssp_port_set_bits(struct ti_ssp *ssp, int port, int reg,
  117. u32 bits)
  118. {
  119. ssp_port_rmw(ssp, port, reg, 0, bits);
  120. }
  121. /* Called to setup port clock mode, caller must hold ssp->lock */
  122. static int __set_mode(struct ti_ssp *ssp, int port, int mode)
  123. {
  124. mode &= SSP_PORT_CONFIG_MASK;
  125. ssp_port_rmw(ssp, port, PORT_CFG_1, SSP_PORT_CONFIG_MASK, mode);
  126. return 0;
  127. }
  128. int ti_ssp_set_mode(struct device *dev, int mode)
  129. {
  130. struct ti_ssp *ssp = dev_to_ssp(dev);
  131. int port = dev_to_port(dev);
  132. int ret;
  133. spin_lock(&ssp->lock);
  134. ret = __set_mode(ssp, port, mode);
  135. spin_unlock(&ssp->lock);
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(ti_ssp_set_mode);
  139. /* Called to setup iosel2, caller must hold ssp->lock */
  140. static void __set_iosel2(struct ti_ssp *ssp, u32 mask, u32 val)
  141. {
  142. ssp->iosel2 = (ssp->iosel2 & ~mask) | val;
  143. ssp_write(ssp, REG_IOSEL_2, ssp->iosel2);
  144. }
  145. /* Called to setup port iosel, caller must hold ssp->lock */
  146. static void __set_iosel(struct ti_ssp *ssp, int port, u32 iosel)
  147. {
  148. unsigned val, shift = port ? 16 : 0;
  149. /* IOSEL1 gets the least significant 16 bits */
  150. val = ssp_read(ssp, REG_IOSEL_1);
  151. val &= 0xffff << (port ? 0 : 16);
  152. val |= (iosel & 0xffff) << (port ? 16 : 0);
  153. ssp_write(ssp, REG_IOSEL_1, val);
  154. /* IOSEL2 gets the most significant 16 bits */
  155. val = (iosel >> 16) & 0x7;
  156. __set_iosel2(ssp, 0x7 << shift, val << shift);
  157. }
  158. int ti_ssp_set_iosel(struct device *dev, u32 iosel)
  159. {
  160. struct ti_ssp *ssp = dev_to_ssp(dev);
  161. int port = dev_to_port(dev);
  162. spin_lock(&ssp->lock);
  163. __set_iosel(ssp, port, iosel);
  164. spin_unlock(&ssp->lock);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(ti_ssp_set_iosel);
  168. int ti_ssp_load(struct device *dev, int offs, u32* prog, int len)
  169. {
  170. struct ti_ssp *ssp = dev_to_ssp(dev);
  171. int port = dev_to_port(dev);
  172. int i;
  173. if (len > SSP_PORT_SEQRAM_SIZE)
  174. return -ENOSPC;
  175. spin_lock(&ssp->lock);
  176. /* Enable SeqRAM access */
  177. ssp_port_set_bits(ssp, port, PORT_CFG_2, SSP_SEQRAM_WR_EN);
  178. /* Copy code */
  179. for (i = 0; i < len; i++) {
  180. __raw_writel(prog[i], ssp->regs + offs + 4*i +
  181. ssp_port_seqram[port]);
  182. }
  183. /* Disable SeqRAM access */
  184. ssp_port_clr_bits(ssp, port, PORT_CFG_2, SSP_SEQRAM_WR_EN);
  185. spin_unlock(&ssp->lock);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(ti_ssp_load);
  189. int ti_ssp_raw_read(struct device *dev)
  190. {
  191. struct ti_ssp *ssp = dev_to_ssp(dev);
  192. int port = dev_to_port(dev);
  193. int shift = port ? 27 : 11;
  194. return (ssp_read(ssp, REG_IOSEL_2) >> shift) & 0xf;
  195. }
  196. EXPORT_SYMBOL(ti_ssp_raw_read);
  197. int ti_ssp_raw_write(struct device *dev, u32 val)
  198. {
  199. struct ti_ssp *ssp = dev_to_ssp(dev);
  200. int port = dev_to_port(dev), shift;
  201. spin_lock(&ssp->lock);
  202. shift = port ? 22 : 6;
  203. val &= 0xf;
  204. __set_iosel2(ssp, 0xf << shift, val << shift);
  205. spin_unlock(&ssp->lock);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(ti_ssp_raw_write);
  209. static inline int __xfer_done(struct ti_ssp *ssp, int port)
  210. {
  211. return !(ssp_port_read(ssp, port, PORT_CFG_1) & SSP_BUSY);
  212. }
  213. int ti_ssp_run(struct device *dev, u32 pc, u32 input, u32 *output)
  214. {
  215. struct ti_ssp *ssp = dev_to_ssp(dev);
  216. int port = dev_to_port(dev);
  217. int ret;
  218. if (pc & ~(0x3f))
  219. return -EINVAL;
  220. /* Grab ssp->lock to serialize rmw on ssp registers */
  221. spin_lock(&ssp->lock);
  222. ssp_port_write(ssp, port, PORT_ADDR, input >> 16);
  223. ssp_port_write(ssp, port, PORT_DATA, input & 0xffff);
  224. ssp_port_rmw(ssp, port, PORT_CFG_1, 0x3f, pc);
  225. /* grab wait queue head lock to avoid race with the isr */
  226. spin_lock_irq(&ssp->wqh.lock);
  227. /* kick off sequence execution in hardware */
  228. ssp_port_set_bits(ssp, port, PORT_CFG_1, SSP_START);
  229. /* drop ssp lock; no register writes beyond this */
  230. spin_unlock(&ssp->lock);
  231. ret = wait_event_interruptible_locked_irq(ssp->wqh,
  232. __xfer_done(ssp, port));
  233. spin_unlock_irq(&ssp->wqh.lock);
  234. if (ret < 0)
  235. return ret;
  236. if (output) {
  237. *output = (ssp_port_read(ssp, port, PORT_ADDR) << 16) |
  238. (ssp_port_read(ssp, port, PORT_DATA) & 0xffff);
  239. }
  240. ret = ssp_port_read(ssp, port, PORT_STATE) & 0x3f; /* stop address */
  241. return ret;
  242. }
  243. EXPORT_SYMBOL(ti_ssp_run);
  244. static irqreturn_t ti_ssp_interrupt(int irq, void *dev_data)
  245. {
  246. struct ti_ssp *ssp = dev_data;
  247. spin_lock(&ssp->wqh.lock);
  248. ssp_write(ssp, REG_INTR_ST, 0x3);
  249. wake_up_locked(&ssp->wqh);
  250. spin_unlock(&ssp->wqh.lock);
  251. return IRQ_HANDLED;
  252. }
  253. static int __devinit ti_ssp_probe(struct platform_device *pdev)
  254. {
  255. static struct ti_ssp *ssp;
  256. const struct ti_ssp_data *pdata = pdev->dev.platform_data;
  257. int error = 0, prediv = 0xff, id;
  258. unsigned long sysclk;
  259. struct device *dev = &pdev->dev;
  260. struct mfd_cell cells[2];
  261. ssp = kzalloc(sizeof(*ssp), GFP_KERNEL);
  262. if (!ssp) {
  263. dev_err(dev, "cannot allocate device info\n");
  264. return -ENOMEM;
  265. }
  266. ssp->dev = dev;
  267. dev_set_drvdata(dev, ssp);
  268. ssp->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. if (!ssp->res) {
  270. error = -ENODEV;
  271. dev_err(dev, "cannot determine register area\n");
  272. goto error_res;
  273. }
  274. if (!request_mem_region(ssp->res->start, resource_size(ssp->res),
  275. pdev->name)) {
  276. error = -ENOMEM;
  277. dev_err(dev, "cannot claim register memory\n");
  278. goto error_res;
  279. }
  280. ssp->regs = ioremap(ssp->res->start, resource_size(ssp->res));
  281. if (!ssp->regs) {
  282. error = -ENOMEM;
  283. dev_err(dev, "cannot map register memory\n");
  284. goto error_map;
  285. }
  286. ssp->clk = clk_get(dev, NULL);
  287. if (IS_ERR(ssp->clk)) {
  288. error = PTR_ERR(ssp->clk);
  289. dev_err(dev, "cannot claim device clock\n");
  290. goto error_clk;
  291. }
  292. ssp->irq = platform_get_irq(pdev, 0);
  293. if (ssp->irq < 0) {
  294. error = -ENODEV;
  295. dev_err(dev, "unknown irq\n");
  296. goto error_irq;
  297. }
  298. error = request_threaded_irq(ssp->irq, NULL, ti_ssp_interrupt, 0,
  299. dev_name(dev), ssp);
  300. if (error < 0) {
  301. dev_err(dev, "cannot acquire irq\n");
  302. goto error_irq;
  303. }
  304. spin_lock_init(&ssp->lock);
  305. init_waitqueue_head(&ssp->wqh);
  306. /* Power on and initialize SSP */
  307. error = clk_enable(ssp->clk);
  308. if (error) {
  309. dev_err(dev, "cannot enable device clock\n");
  310. goto error_enable;
  311. }
  312. /* Reset registers to a sensible known state */
  313. ssp_write(ssp, REG_IOSEL_1, 0);
  314. ssp_write(ssp, REG_IOSEL_2, 0);
  315. ssp_write(ssp, REG_INTR_EN, 0x3);
  316. ssp_write(ssp, REG_INTR_ST, 0x3);
  317. ssp_write(ssp, REG_TEST_CTRL, 0);
  318. ssp_port_write(ssp, 0, PORT_CFG_1, SSP_PORT_ASL);
  319. ssp_port_write(ssp, 1, PORT_CFG_1, SSP_PORT_ASL);
  320. ssp_port_write(ssp, 0, PORT_CFG_2, SSP_PORT_CFO1);
  321. ssp_port_write(ssp, 1, PORT_CFG_2, SSP_PORT_CFO1);
  322. sysclk = clk_get_rate(ssp->clk);
  323. if (pdata && pdata->out_clock)
  324. prediv = (sysclk / pdata->out_clock) - 1;
  325. prediv = clamp(prediv, 0, 0xff);
  326. ssp_rmw(ssp, REG_PREDIV, 0xff, prediv);
  327. memset(cells, 0, sizeof(cells));
  328. for (id = 0; id < 2; id++) {
  329. const struct ti_ssp_dev_data *data = &pdata->dev_data[id];
  330. cells[id].id = id;
  331. cells[id].name = data->dev_name;
  332. cells[id].platform_data = data->pdata;
  333. cells[id].data_size = data->pdata_size;
  334. }
  335. error = mfd_add_devices(dev, 0, cells, 2, NULL, 0);
  336. if (error < 0) {
  337. dev_err(dev, "cannot add mfd cells\n");
  338. goto error_enable;
  339. }
  340. return 0;
  341. error_enable:
  342. free_irq(ssp->irq, ssp);
  343. error_irq:
  344. clk_put(ssp->clk);
  345. error_clk:
  346. iounmap(ssp->regs);
  347. error_map:
  348. release_mem_region(ssp->res->start, resource_size(ssp->res));
  349. error_res:
  350. kfree(ssp);
  351. return error;
  352. }
  353. static int __devexit ti_ssp_remove(struct platform_device *pdev)
  354. {
  355. struct device *dev = &pdev->dev;
  356. struct ti_ssp *ssp = dev_get_drvdata(dev);
  357. mfd_remove_devices(dev);
  358. clk_disable(ssp->clk);
  359. free_irq(ssp->irq, ssp);
  360. clk_put(ssp->clk);
  361. iounmap(ssp->regs);
  362. release_mem_region(ssp->res->start, resource_size(ssp->res));
  363. kfree(ssp);
  364. dev_set_drvdata(dev, NULL);
  365. return 0;
  366. }
  367. static struct platform_driver ti_ssp_driver = {
  368. .probe = ti_ssp_probe,
  369. .remove = __devexit_p(ti_ssp_remove),
  370. .driver = {
  371. .name = "ti-ssp",
  372. .owner = THIS_MODULE,
  373. }
  374. };
  375. module_platform_driver(ti_ssp_driver);
  376. MODULE_DESCRIPTION("Sequencer Serial Port (SSP) Driver");
  377. MODULE_AUTHOR("Cyril Chemparathy");
  378. MODULE_LICENSE("GPL");
  379. MODULE_ALIAS("platform:ti-ssp");