i2c-iop3xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* ------------------------------------------------------------------------- */
  3. /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
  4. /* ------------------------------------------------------------------------- */
  5. /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
  6. * <Peter dot Milne at D hyphen TACQ dot com>
  7. *
  8. * With acknowledgements to i2c-algo-ibm_ocp.c by
  9. * Ian DaSilva, MontaVista Software, Inc. [email protected]
  10. *
  11. * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
  12. *
  13. * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
  14. *
  15. * And which acknowledged Kyösti Mälkki <[email protected]>,
  16. * Frodo Looijaard <[email protected]>, Martin Bailey<[email protected]>
  17. *
  18. * Major cleanup by Deepak Saxena <[email protected]>, 01/2005:
  19. *
  20. * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
  21. * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
  22. * - Make it work with IXP46x chips
  23. * - Cleanup function names, coding style, etc
  24. *
  25. * - writing to slave address causes latchup on iop331.
  26. * fix: driver refuses to address self.
  27. */
  28. #include <linux/interrupt.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/errno.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/i2c.h>
  36. #include <linux/io.h>
  37. #include <linux/gpio/consumer.h>
  38. #include "i2c-iop3xx.h"
  39. /* global unit counter */
  40. static int i2c_id;
  41. static inline unsigned char
  42. iic_cook_addr(struct i2c_msg *msg)
  43. {
  44. unsigned char addr;
  45. addr = i2c_8bit_addr_from_msg(msg);
  46. return addr;
  47. }
  48. static void
  49. iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
  50. {
  51. /* Follows devman 9.3 */
  52. __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
  53. __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
  54. __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
  55. }
  56. static void
  57. iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
  58. {
  59. u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
  60. /*
  61. * Every time unit enable is asserted, GPOD needs to be cleared
  62. * on IOP3XX to avoid data corruption on the bus. We use the
  63. * gpiod_set_raw_value() to make sure the 0 hits the hardware
  64. * GPOD register. These descriptors are only passed along to
  65. * the device if this is necessary.
  66. */
  67. if (iop3xx_adap->gpio_scl)
  68. gpiod_set_raw_value(iop3xx_adap->gpio_scl, 0);
  69. if (iop3xx_adap->gpio_sda)
  70. gpiod_set_raw_value(iop3xx_adap->gpio_sda, 0);
  71. /* NB SR bits not same position as CR IE bits :-( */
  72. iop3xx_adap->SR_enabled =
  73. IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
  74. IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
  75. cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
  76. IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
  77. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  78. }
  79. static void
  80. iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
  81. {
  82. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  83. cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
  84. IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
  85. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  86. }
  87. /*
  88. * NB: the handler has to clear the source of the interrupt!
  89. * Then it passes the SR flags of interest to BH via adap data
  90. */
  91. static irqreturn_t
  92. iop3xx_i2c_irq_handler(int this_irq, void *dev_id)
  93. {
  94. struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
  95. u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
  96. if ((sr &= iop3xx_adap->SR_enabled)) {
  97. __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
  98. iop3xx_adap->SR_received |= sr;
  99. wake_up_interruptible(&iop3xx_adap->waitq);
  100. }
  101. return IRQ_HANDLED;
  102. }
  103. /* check all error conditions, clear them , report most important */
  104. static int
  105. iop3xx_i2c_error(u32 sr)
  106. {
  107. int rc = 0;
  108. if ((sr & IOP3XX_ISR_BERRD)) {
  109. if (!rc)
  110. rc = -I2C_ERR_BERR;
  111. }
  112. if ((sr & IOP3XX_ISR_ALD)) {
  113. if (!rc)
  114. rc = -I2C_ERR_ALD;
  115. }
  116. return rc;
  117. }
  118. static inline u32
  119. iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
  120. {
  121. unsigned long flags;
  122. u32 sr;
  123. spin_lock_irqsave(&iop3xx_adap->lock, flags);
  124. sr = iop3xx_adap->SR_received;
  125. iop3xx_adap->SR_received = 0;
  126. spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
  127. return sr;
  128. }
  129. /*
  130. * sleep until interrupted, then recover and analyse the SR
  131. * saved by handler
  132. */
  133. typedef int (*compare_func)(unsigned test, unsigned mask);
  134. /* returns 1 on correct comparison */
  135. static int
  136. iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
  137. unsigned flags, unsigned *status,
  138. compare_func compare)
  139. {
  140. unsigned sr = 0;
  141. int interrupted;
  142. int done;
  143. int rc = 0;
  144. do {
  145. interrupted = wait_event_interruptible_timeout (
  146. iop3xx_adap->waitq,
  147. (done = compare(sr = iop3xx_i2c_get_srstat(iop3xx_adap), flags)),
  148. 1 * HZ
  149. );
  150. if ((rc = iop3xx_i2c_error(sr)) < 0) {
  151. *status = sr;
  152. return rc;
  153. } else if (!interrupted) {
  154. *status = sr;
  155. return -ETIMEDOUT;
  156. }
  157. } while (!done);
  158. *status = sr;
  159. return 0;
  160. }
  161. /*
  162. * Concrete compare_funcs
  163. */
  164. static int
  165. all_bits_clear(unsigned test, unsigned mask)
  166. {
  167. return (test & mask) == 0;
  168. }
  169. static int
  170. any_bits_set(unsigned test, unsigned mask)
  171. {
  172. return (test & mask) != 0;
  173. }
  174. static int
  175. iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  176. {
  177. return iop3xx_i2c_wait_event(
  178. iop3xx_adap,
  179. IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
  180. status, any_bits_set);
  181. }
  182. static int
  183. iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  184. {
  185. return iop3xx_i2c_wait_event(
  186. iop3xx_adap,
  187. IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
  188. status, any_bits_set);
  189. }
  190. static int
  191. iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  192. {
  193. return iop3xx_i2c_wait_event(
  194. iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
  195. }
  196. static int
  197. iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
  198. struct i2c_msg *msg)
  199. {
  200. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  201. int status;
  202. int rc;
  203. /* avoid writing to my slave address (hangs on 80331),
  204. * forbidden in Intel developer manual
  205. */
  206. if (msg->addr == MYSAR) {
  207. return -EBUSY;
  208. }
  209. __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
  210. cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
  211. cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
  212. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  213. rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
  214. return rc;
  215. }
  216. static int
  217. iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
  218. int stop)
  219. {
  220. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  221. int status;
  222. int rc = 0;
  223. __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
  224. cr &= ~IOP3XX_ICR_MSTART;
  225. if (stop) {
  226. cr |= IOP3XX_ICR_MSTOP;
  227. } else {
  228. cr &= ~IOP3XX_ICR_MSTOP;
  229. }
  230. cr |= IOP3XX_ICR_TBYTE;
  231. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  232. rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
  233. return rc;
  234. }
  235. static int
  236. iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char *byte,
  237. int stop)
  238. {
  239. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  240. int status;
  241. int rc = 0;
  242. cr &= ~IOP3XX_ICR_MSTART;
  243. if (stop) {
  244. cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
  245. } else {
  246. cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
  247. }
  248. cr |= IOP3XX_ICR_TBYTE;
  249. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  250. rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
  251. *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
  252. return rc;
  253. }
  254. static int
  255. iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
  256. {
  257. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  258. int ii;
  259. int rc = 0;
  260. for (ii = 0; rc == 0 && ii != count; ++ii)
  261. rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii == count-1);
  262. return rc;
  263. }
  264. static int
  265. iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
  266. {
  267. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  268. int ii;
  269. int rc = 0;
  270. for (ii = 0; rc == 0 && ii != count; ++ii)
  271. rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii == count-1);
  272. return rc;
  273. }
  274. /*
  275. * Description: This function implements combined transactions. Combined
  276. * transactions consist of combinations of reading and writing blocks of data.
  277. * FROM THE SAME ADDRESS
  278. * Each transfer (i.e. a read or a write) is separated by a repeated start
  279. * condition.
  280. */
  281. static int
  282. iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg *pmsg)
  283. {
  284. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  285. int rc;
  286. rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
  287. if (rc < 0) {
  288. return rc;
  289. }
  290. if ((pmsg->flags&I2C_M_RD)) {
  291. return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
  292. } else {
  293. return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
  294. }
  295. }
  296. /*
  297. * master_xfer() - main read/write entry
  298. */
  299. static int
  300. iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
  301. int num)
  302. {
  303. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  304. int im = 0;
  305. int ret = 0;
  306. int status;
  307. iop3xx_i2c_wait_idle(iop3xx_adap, &status);
  308. iop3xx_i2c_reset(iop3xx_adap);
  309. iop3xx_i2c_enable(iop3xx_adap);
  310. for (im = 0; ret == 0 && im != num; im++) {
  311. ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
  312. }
  313. iop3xx_i2c_transaction_cleanup(iop3xx_adap);
  314. if (ret)
  315. return ret;
  316. return im;
  317. }
  318. static u32
  319. iop3xx_i2c_func(struct i2c_adapter *adap)
  320. {
  321. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  322. }
  323. static const struct i2c_algorithm iop3xx_i2c_algo = {
  324. .master_xfer = iop3xx_i2c_master_xfer,
  325. .functionality = iop3xx_i2c_func,
  326. };
  327. static int
  328. iop3xx_i2c_remove(struct platform_device *pdev)
  329. {
  330. struct i2c_adapter *padapter = platform_get_drvdata(pdev);
  331. struct i2c_algo_iop3xx_data *adapter_data =
  332. (struct i2c_algo_iop3xx_data *)padapter->algo_data;
  333. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  334. unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
  335. /*
  336. * Disable the actual HW unit
  337. */
  338. cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
  339. IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
  340. __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
  341. iounmap(adapter_data->ioaddr);
  342. release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
  343. kfree(adapter_data);
  344. kfree(padapter);
  345. return 0;
  346. }
  347. static int
  348. iop3xx_i2c_probe(struct platform_device *pdev)
  349. {
  350. struct resource *res;
  351. int ret, irq;
  352. struct i2c_adapter *new_adapter;
  353. struct i2c_algo_iop3xx_data *adapter_data;
  354. new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  355. if (!new_adapter) {
  356. ret = -ENOMEM;
  357. goto out;
  358. }
  359. adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
  360. if (!adapter_data) {
  361. ret = -ENOMEM;
  362. goto free_adapter;
  363. }
  364. adapter_data->gpio_scl = devm_gpiod_get_optional(&pdev->dev,
  365. "scl",
  366. GPIOD_ASIS);
  367. if (IS_ERR(adapter_data->gpio_scl)) {
  368. ret = PTR_ERR(adapter_data->gpio_scl);
  369. goto free_both;
  370. }
  371. adapter_data->gpio_sda = devm_gpiod_get_optional(&pdev->dev,
  372. "sda",
  373. GPIOD_ASIS);
  374. if (IS_ERR(adapter_data->gpio_sda)) {
  375. ret = PTR_ERR(adapter_data->gpio_sda);
  376. goto free_both;
  377. }
  378. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  379. if (!res) {
  380. ret = -ENODEV;
  381. goto free_both;
  382. }
  383. if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
  384. ret = -EBUSY;
  385. goto free_both;
  386. }
  387. /* set the adapter enumeration # */
  388. adapter_data->id = i2c_id++;
  389. adapter_data->ioaddr = ioremap(res->start, IOP3XX_I2C_IO_SIZE);
  390. if (!adapter_data->ioaddr) {
  391. ret = -ENOMEM;
  392. goto release_region;
  393. }
  394. irq = platform_get_irq(pdev, 0);
  395. if (irq < 0) {
  396. ret = irq;
  397. goto unmap;
  398. }
  399. ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
  400. pdev->name, adapter_data);
  401. if (ret)
  402. goto unmap;
  403. memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
  404. new_adapter->owner = THIS_MODULE;
  405. new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  406. new_adapter->dev.parent = &pdev->dev;
  407. new_adapter->dev.of_node = pdev->dev.of_node;
  408. new_adapter->nr = pdev->id;
  409. /*
  410. * Default values...should these come in from board code?
  411. */
  412. new_adapter->timeout = HZ;
  413. new_adapter->algo = &iop3xx_i2c_algo;
  414. init_waitqueue_head(&adapter_data->waitq);
  415. spin_lock_init(&adapter_data->lock);
  416. iop3xx_i2c_reset(adapter_data);
  417. iop3xx_i2c_enable(adapter_data);
  418. platform_set_drvdata(pdev, new_adapter);
  419. new_adapter->algo_data = adapter_data;
  420. i2c_add_numbered_adapter(new_adapter);
  421. return 0;
  422. unmap:
  423. iounmap(adapter_data->ioaddr);
  424. release_region:
  425. release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
  426. free_both:
  427. kfree(adapter_data);
  428. free_adapter:
  429. kfree(new_adapter);
  430. out:
  431. return ret;
  432. }
  433. static const struct of_device_id i2c_iop3xx_match[] = {
  434. { .compatible = "intel,iop3xx-i2c", },
  435. { .compatible = "intel,ixp4xx-i2c", },
  436. {},
  437. };
  438. MODULE_DEVICE_TABLE(of, i2c_iop3xx_match);
  439. static struct platform_driver iop3xx_i2c_driver = {
  440. .probe = iop3xx_i2c_probe,
  441. .remove = iop3xx_i2c_remove,
  442. .driver = {
  443. .name = "IOP3xx-I2C",
  444. .of_match_table = i2c_iop3xx_match,
  445. },
  446. };
  447. module_platform_driver(iop3xx_i2c_driver);
  448. MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
  449. MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
  450. MODULE_LICENSE("GPL");
  451. MODULE_ALIAS("platform:IOP3xx-I2C");