0044-i2c-MIPS-adds-ralink-I2C-driver.patch 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. From 723b8beaabf3c3c4b1ce69480141f1e926f3f3b2 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sun, 27 Jul 2014 09:52:56 +0100
  4. Subject: [PATCH 44/53] i2c: MIPS: adds ralink I2C driver
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. .../devicetree/bindings/i2c/i2c-ralink.txt | 27 ++
  8. drivers/i2c/busses/Kconfig | 4 +
  9. drivers/i2c/busses/Makefile | 1 +
  10. drivers/i2c/busses/i2c-ralink.c | 327 ++++++++++++++++++++
  11. 4 files changed, 359 insertions(+)
  12. create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ralink.txt
  13. create mode 100644 drivers/i2c/busses/i2c-ralink.c
  14. --- /dev/null
  15. +++ b/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
  16. @@ -0,0 +1,27 @@
  17. +I2C for Ralink platforms
  18. +
  19. +Required properties :
  20. +- compatible : Must be "link,rt3052-i2c"
  21. +- reg: physical base address of the controller and length of memory mapped
  22. + region.
  23. +- #address-cells = <1>;
  24. +- #size-cells = <0>;
  25. +
  26. +Optional properties:
  27. +- Child nodes conforming to i2c bus binding
  28. +
  29. +Example :
  30. +
  31. +palmbus@10000000 {
  32. + i2c@900 {
  33. + compatible = "link,rt3052-i2c";
  34. + reg = <0x900 0x100>;
  35. + #address-cells = <1>;
  36. + #size-cells = <0>;
  37. +
  38. + hwmon@4b {
  39. + compatible = "national,lm92";
  40. + reg = <0x4b>;
  41. + };
  42. + };
  43. +};
  44. --- a/drivers/i2c/busses/Kconfig
  45. +++ b/drivers/i2c/busses/Kconfig
  46. @@ -806,6 +806,10 @@ config I2C_RK3X
  47. This driver can also be built as a module. If so, the module will
  48. be called i2c-rk3x.
  49. +config I2C_RALINK
  50. + tristate "Ralink I2C Controller"
  51. + select OF_I2C
  52. +
  53. config HAVE_S3C2410_I2C
  54. bool
  55. help
  56. --- a/drivers/i2c/busses/Makefile
  57. +++ b/drivers/i2c/busses/Makefile
  58. @@ -75,6 +75,7 @@ obj-$(CONFIG_I2C_PNX) += i2c-pnx.o
  59. obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o
  60. obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
  61. obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
  62. +obj-$(CONFIG_I2C_RALINK) += i2c-ralink.o
  63. obj-$(CONFIG_I2C_QUP) += i2c-qup.o
  64. obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
  65. obj-$(CONFIG_I2C_RK3X) += i2c-rk3x.o
  66. --- /dev/null
  67. +++ b/drivers/i2c/busses/i2c-ralink.c
  68. @@ -0,0 +1,327 @@
  69. +/*
  70. + * drivers/i2c/busses/i2c-ralink.c
  71. + *
  72. + * Copyright (C) 2013 Steven Liu <[email protected]>
  73. + *
  74. + * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
  75. + * (C) 2014 Sittisak <[email protected]>
  76. + *
  77. + * This software is licensed under the terms of the GNU General Public
  78. + * License version 2, as published by the Free Software Foundation, and
  79. + * may be copied, distributed, and modified under those terms.
  80. + *
  81. + * This program is distributed in the hope that it will be useful,
  82. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  83. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  84. + * GNU General Public License for more details.
  85. + *
  86. + */
  87. +
  88. +#include <linux/interrupt.h>
  89. +#include <linux/kernel.h>
  90. +#include <linux/module.h>
  91. +#include <linux/reset.h>
  92. +#include <linux/delay.h>
  93. +#include <linux/slab.h>
  94. +#include <linux/init.h>
  95. +#include <linux/errno.h>
  96. +#include <linux/platform_device.h>
  97. +#include <linux/of_platform.h>
  98. +#include <linux/i2c.h>
  99. +#include <linux/io.h>
  100. +#include <linux/err.h>
  101. +
  102. +#include <asm/mach-ralink/ralink_regs.h>
  103. +
  104. +#define REG_CONFIG_REG 0x00
  105. +#define REG_CLKDIV_REG 0x04
  106. +#define REG_DEVADDR_REG 0x08
  107. +#define REG_ADDR_REG 0x0C
  108. +#define REG_DATAOUT_REG 0x10
  109. +#define REG_DATAIN_REG 0x14
  110. +#define REG_STATUS_REG 0x18
  111. +#define REG_STARTXFR_REG 0x1C
  112. +#define REG_BYTECNT_REG 0x20
  113. +#define REG_SM0CFG2 0x28
  114. +#define REG_SM0CTL0 0x40
  115. +
  116. +#define I2C_STARTERR BIT(4)
  117. +#define I2C_ACKERR BIT(3)
  118. +#define I2C_DATARDY BIT(2)
  119. +#define I2C_SDOEMPTY BIT(1)
  120. +#define I2C_BUSY BIT(0)
  121. +
  122. +#define I2C_DEVADLEN_7 (6 << 2)
  123. +#define I2C_ADDRDIS BIT(1)
  124. +
  125. +#define CLKDIV_VALUE 200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
  126. +//#define CLKDIV_VALUE 50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
  127. +
  128. +#define READ_CMD 0x01
  129. +#define WRITE_CMD 0x00
  130. +#define READ_BLOCK 64
  131. +
  132. +#define SM0CTL0_OD BIT(31)
  133. +#define SM0CTL0_VTRIG BIT(28)
  134. +#define SM0CTL0_OUTHI BIT(6)
  135. +#define SM0CTL0_STRETCH BIT(1)
  136. +#define SM0CTL0_DEFAULT (SM0CTL0_OD | SM0CTL0_VTRIG | SM0CTL0_OUTHI | SM0CTL0_STRETCH)
  137. +
  138. +/* timeout waiting for I2C devices to respond (clock streching) */
  139. +#define RT_I2C_TIMEOUT (msecs_to_jiffies(1000))
  140. +
  141. +enum {
  142. + I2C_TYPE_RALINK,
  143. + I2C_TYPE_MEDIATEK,
  144. +};
  145. +
  146. +static void __iomem *membase;
  147. +static struct i2c_adapter *adapter;
  148. +static int hw_type;
  149. +
  150. +static void rt_i2c_w32(u32 val, unsigned reg)
  151. +{
  152. + iowrite32(val, membase + reg);
  153. +}
  154. +
  155. +static u32 rt_i2c_r32(unsigned reg)
  156. +{
  157. + return ioread32(membase + reg);
  158. +}
  159. +
  160. +static inline int rt_i2c_get_ack(void)
  161. +{
  162. + return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
  163. +}
  164. +
  165. +static inline int rt_i2c_wait_rx_done(void)
  166. +{
  167. + unsigned long timeout;
  168. +
  169. + timeout = jiffies + RT_I2C_TIMEOUT;
  170. +
  171. + do {
  172. + if (time_after(jiffies, timeout))
  173. + return (-ETIMEDOUT);
  174. +
  175. + } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
  176. +
  177. + return 0;
  178. +}
  179. +
  180. +static inline int rt_i2c_wait_idle(void)
  181. +{
  182. + unsigned long timeout;
  183. +
  184. + timeout = jiffies + RT_I2C_TIMEOUT;
  185. +
  186. + do {
  187. + if (time_after(jiffies, timeout)) {
  188. + printk("i2c-read line busy\n");
  189. + return 1;
  190. + }
  191. + } while (rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
  192. +
  193. + return 0;
  194. +}
  195. +
  196. +static inline int rt_i2c_wait_tx_done(void)
  197. +{
  198. + unsigned long timeout;
  199. +
  200. + timeout = jiffies + RT_I2C_TIMEOUT;
  201. +
  202. + do {
  203. + if (time_after(jiffies, timeout))
  204. + return (-ETIMEDOUT);
  205. +
  206. + } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
  207. +
  208. + return 0;
  209. +}
  210. +
  211. +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
  212. +{
  213. + int i = 0, j = 0, pos = 0;
  214. + int nblock = msg->len / READ_BLOCK;
  215. + int rem = msg->len % READ_BLOCK;
  216. + int ret = 0;
  217. +
  218. + if (msg->flags & I2C_M_TEN) {
  219. + printk("10 bits addr not supported\n");
  220. + return -EINVAL;
  221. + }
  222. +
  223. + if (msg->flags & I2C_M_RD) {
  224. + for (i = 0; i < nblock; i++) {
  225. + if (rt_i2c_wait_idle())
  226. + return -ETIMEDOUT;
  227. + rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
  228. + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
  229. + for (j = 0; j < READ_BLOCK; j++) {
  230. + if (rt_i2c_wait_rx_done() < 0)
  231. + ret = rt_i2c_wait_rx_done();
  232. + if (rt_i2c_get_ack() < 0)
  233. + ret = rt_i2c_get_ack();
  234. + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
  235. + }
  236. + }
  237. +
  238. + if (rt_i2c_wait_idle())
  239. + return -ETIMEDOUT;
  240. + if (rem) {
  241. + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
  242. + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
  243. + }
  244. + for (i = 0; i < rem; i++) {
  245. + if (rt_i2c_wait_rx_done() < 0)
  246. + ret = rt_i2c_wait_rx_done();
  247. + if (rt_i2c_get_ack() < 0)
  248. + ret = rt_i2c_get_ack();
  249. +
  250. + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
  251. + }
  252. + } else {
  253. + if (rt_i2c_wait_idle())
  254. + return -ETIMEDOUT;
  255. + rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
  256. + for (i = 0; i < msg->len; i++) {
  257. + rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
  258. + rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
  259. +
  260. + if (rt_i2c_wait_tx_done() < 0)
  261. + ret = rt_i2c_wait_tx_done();
  262. + if (rt_i2c_get_ack() < 0)
  263. + ret = rt_i2c_get_ack();
  264. + }
  265. + }
  266. +
  267. + return ret;
  268. +}
  269. +
  270. +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
  271. +{
  272. + int i = 0;
  273. + int ret = 0;
  274. +
  275. + if (rt_i2c_wait_idle())
  276. + return -ETIMEDOUT;
  277. +
  278. + device_reset(a->dev.parent);
  279. +
  280. + rt_i2c_w32(m->addr, REG_DEVADDR_REG);
  281. + rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
  282. + if (hw_type == I2C_TYPE_RALINK) {
  283. + rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
  284. + } else {
  285. + rt_i2c_w32((CLKDIV_VALUE << 16) | SM0CTL0_DEFAULT, REG_SM0CTL0);
  286. + rt_i2c_w32(1, REG_SM0CFG2);
  287. + }
  288. +
  289. + for (i = 0; i < n && !ret; i++) {
  290. + ret = rt_i2c_handle_msg(a, &m[i]);
  291. +
  292. + if (ret < 0) {
  293. + return ret;
  294. + }
  295. + }
  296. +
  297. + return n;
  298. +}
  299. +
  300. +static u32 rt_i2c_func(struct i2c_adapter *a)
  301. +{
  302. + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  303. +}
  304. +
  305. +static const struct i2c_algorithm rt_i2c_algo = {
  306. + .master_xfer = rt_i2c_master_xfer,
  307. + .functionality = rt_i2c_func,
  308. +};
  309. +
  310. +static const struct of_device_id i2c_rt_dt_ids[] = {
  311. + { .compatible = "ralink,rt2880-i2c", .data = (void *) I2C_TYPE_RALINK },
  312. + { .compatible = "mediatek,mt7628-i2c", .data = (void *) I2C_TYPE_MEDIATEK },
  313. + { /* sentinel */ }
  314. +};
  315. +
  316. +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
  317. +
  318. +static int rt_i2c_probe(struct platform_device *pdev)
  319. +{
  320. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. + const struct of_device_id *match;
  322. + int ret;
  323. +
  324. + match = of_match_device(i2c_rt_dt_ids, &pdev->dev);
  325. + hw_type = (int) match->data;
  326. +
  327. + if (!res) {
  328. + dev_err(&pdev->dev, "no memory resource found\n");
  329. + return -ENODEV;
  330. + }
  331. +
  332. + adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
  333. + if (!adapter) {
  334. + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
  335. + return -ENOMEM;
  336. + }
  337. +
  338. + membase = devm_ioremap_resource(&pdev->dev, res);
  339. + if (IS_ERR(membase))
  340. + return PTR_ERR(membase);
  341. +
  342. + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
  343. + adapter->owner = THIS_MODULE;
  344. + adapter->nr = pdev->id;
  345. + adapter->timeout = HZ;
  346. + adapter->algo = &rt_i2c_algo;
  347. + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  348. + adapter->dev.parent = &pdev->dev;
  349. + adapter->dev.of_node = pdev->dev.of_node;
  350. +
  351. + ret = i2c_add_numbered_adapter(adapter);
  352. + if (ret)
  353. + return ret;
  354. +
  355. + platform_set_drvdata(pdev, adapter);
  356. +
  357. + dev_info(&pdev->dev, "loaded\n");
  358. +
  359. + return 0;
  360. +}
  361. +
  362. +static int rt_i2c_remove(struct platform_device *pdev)
  363. +{
  364. + platform_set_drvdata(pdev, NULL);
  365. +
  366. + return 0;
  367. +}
  368. +
  369. +static struct platform_driver rt_i2c_driver = {
  370. + .probe = rt_i2c_probe,
  371. + .remove = rt_i2c_remove,
  372. + .driver = {
  373. + .owner = THIS_MODULE,
  374. + .name = "i2c-ralink",
  375. + .of_match_table = i2c_rt_dt_ids,
  376. + },
  377. +};
  378. +
  379. +static int __init i2c_rt_init (void)
  380. +{
  381. + return platform_driver_register(&rt_i2c_driver);
  382. +}
  383. +subsys_initcall(i2c_rt_init);
  384. +
  385. +static void __exit i2c_rt_exit (void)
  386. +{
  387. + platform_driver_unregister(&rt_i2c_driver);
  388. +}
  389. +
  390. +module_exit (i2c_rt_exit);
  391. +
  392. +MODULE_AUTHOR("Steven Liu <[email protected]>");
  393. +MODULE_DESCRIPTION("Ralink I2c host driver");
  394. +MODULE_LICENSE("GPL");
  395. +MODULE_ALIAS("platform:Ralink-I2C");