0045-i2c-add-mt7621-driver.patch 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. From d5c54ff3d1db0a4348fa04d8e78f3bf6063e3afc Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Mon, 7 Dec 2015 17:21:27 +0100
  4. Subject: [PATCH 45/53] i2c: add mt7621 driver
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. drivers/i2c/busses/Kconfig | 4 +
  8. drivers/i2c/busses/Makefile | 1 +
  9. drivers/i2c/busses/i2c-mt7621.c | 303 +++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 308 insertions(+)
  11. create mode 100644 drivers/i2c/busses/i2c-mt7621.c
  12. --- a/drivers/i2c/busses/Kconfig
  13. +++ b/drivers/i2c/busses/Kconfig
  14. @@ -810,6 +810,10 @@ config I2C_RALINK
  15. tristate "Ralink I2C Controller"
  16. select OF_I2C
  17. +config I2C_MT7621
  18. + tristate "MT7621 I2C Controller"
  19. + select OF_I2C
  20. +
  21. config HAVE_S3C2410_I2C
  22. bool
  23. help
  24. --- a/drivers/i2c/busses/Makefile
  25. +++ b/drivers/i2c/busses/Makefile
  26. @@ -76,6 +76,7 @@ obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o
  27. obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
  28. obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
  29. obj-$(CONFIG_I2C_RALINK) += i2c-ralink.o
  30. +obj-$(CONFIG_I2C_MT7621) += i2c-mt7621.o
  31. obj-$(CONFIG_I2C_QUP) += i2c-qup.o
  32. obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
  33. obj-$(CONFIG_I2C_RK3X) += i2c-rk3x.o
  34. --- /dev/null
  35. +++ b/drivers/i2c/busses/i2c-mt7621.c
  36. @@ -0,0 +1,303 @@
  37. +/*
  38. + * drivers/i2c/busses/i2c-mt7621.c
  39. + *
  40. + * Copyright (C) 2013 Steven Liu <[email protected]>
  41. + *
  42. + * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
  43. + * (C) 2014 Sittisak <[email protected]>
  44. + *
  45. + * This software is licensed under the terms of the GNU General Public
  46. + * License version 2, as published by the Free Software Foundation, and
  47. + * may be copied, distributed, and modified under those terms.
  48. + *
  49. + * This program is distributed in the hope that it will be useful,
  50. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. + * GNU General Public License for more details.
  53. + *
  54. + */
  55. +
  56. +#include <linux/interrupt.h>
  57. +#include <linux/kernel.h>
  58. +#include <linux/module.h>
  59. +#include <linux/reset.h>
  60. +#include <linux/delay.h>
  61. +#include <linux/slab.h>
  62. +#include <linux/init.h>
  63. +#include <linux/errno.h>
  64. +#include <linux/platform_device.h>
  65. +#include <linux/i2c.h>
  66. +#include <linux/io.h>
  67. +#include <linux/err.h>
  68. +
  69. +#include <asm/mach-ralink/ralink_regs.h>
  70. +
  71. +#define REG_CONFIG_REG 0x00
  72. +#define REG_CLKDIV_REG 0x04
  73. +#define REG_DEVADDR_REG 0x08
  74. +#define REG_ADDR_REG 0x0C
  75. +#define REG_DATAOUT_REG 0x10
  76. +#define REG_DATAIN_REG 0x14
  77. +#define REG_STATUS_REG 0x18
  78. +#define REG_STARTXFR_REG 0x1C
  79. +#define REG_BYTECNT_REG 0x20
  80. +#define REG_SM0_IS_AUTOMODE 0x28
  81. +#define REG_SM0CTL0 0x40
  82. +
  83. +
  84. +#define I2C_STARTERR 0x10
  85. +#define I2C_ACKERR 0x08
  86. +#define I2C_DATARDY 0x04
  87. +#define I2C_SDOEMPTY 0x02
  88. +#define I2C_BUSY 0x01
  89. +
  90. +/* I2C_CFG register bit field */
  91. +#define I2C_CFG_ADDRLEN_8 (7<<5) /* 8 bits */
  92. +#define I2C_CFG_DEVADLEN_7 (6<<2)
  93. +#define I2C_CFG_ADDRDIS BIT(1)
  94. +#define I2C_CFG_DEVADDIS BIT(0)
  95. +
  96. +#define I2C_CFG_DEFAULT (I2C_CFG_ADDRLEN_8 | \
  97. + I2C_CFG_DEVADLEN_7 | \
  98. + I2C_CFG_ADDRDIS)
  99. +
  100. +#define I2C_RETRY 0x1000
  101. +
  102. +#define CLKDIV_VALUE 333
  103. +#define i2c_busy_loop (CLKDIV_VALUE*30)
  104. +
  105. +#define READ_CMD 0x01
  106. +#define WRITE_CMD 0x00
  107. +#define READ_BLOCK 16
  108. +
  109. +#define SM0_ODRAIN BIT(31)
  110. +#define SM0_VSYNC_MODE BIT(28)
  111. +#define SM0_CLK_DIV (CLKDIV_VALUE << 16)
  112. +#define SM0_WAIT_LEVEL BIT(6)
  113. +#define SM0_EN BIT(1)
  114. +
  115. +#define SM0_CFG_DEFUALT (SM0_ODRAIN | SM0_VSYNC_MODE | \
  116. + SM0_CLK_DIV | SM0_WAIT_LEVEL | \
  117. + SM0_EN)
  118. +/***********************************************************/
  119. +
  120. +static void __iomem *membase;
  121. +static struct i2c_adapter *adapter;
  122. +
  123. +static void rt_i2c_w32(u32 val, unsigned reg)
  124. +{
  125. + iowrite32(val, membase + reg);
  126. +}
  127. +
  128. +static u32 rt_i2c_r32(unsigned reg)
  129. +{
  130. + return ioread32(membase + reg);
  131. +}
  132. +
  133. +static void mt7621_i2c_reset(struct i2c_adapter *a)
  134. +{
  135. + device_reset(a->dev.parent);
  136. +}
  137. +static void mt7621_i2c_enable(struct i2c_msg *msg)
  138. +{
  139. + rt_i2c_w32(msg->addr,REG_DEVADDR_REG);
  140. + rt_i2c_w32(0,REG_ADDR_REG);
  141. +}
  142. +
  143. +static void i2c_master_init(struct i2c_adapter *a)
  144. +{
  145. + mt7621_i2c_reset(a);
  146. + rt_i2c_w32(I2C_CFG_DEFAULT,REG_CONFIG_REG);
  147. + rt_i2c_w32(SM0_CFG_DEFUALT,REG_SM0CTL0);
  148. + rt_i2c_w32(1,REG_SM0_IS_AUTOMODE);//auto mode
  149. +}
  150. +
  151. +
  152. +static inline int rt_i2c_wait_rx_done(void)
  153. +{
  154. + int i=0;
  155. + while((!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY)) && (i<i2c_busy_loop))
  156. + i++;
  157. + if(i>=i2c_busy_loop){
  158. + pr_err("err,wait for idle timeout");
  159. + return -ETIMEDOUT;
  160. + }
  161. + return 0;
  162. +}
  163. +
  164. +static inline int rt_i2c_wait_idle(void)
  165. +{
  166. + int i=0;
  167. + while((rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY) && (i<i2c_busy_loop))
  168. + i++;
  169. + if(i>=i2c_busy_loop){
  170. + pr_err("err,wait for idle timeout");
  171. + return -ETIMEDOUT;
  172. + }
  173. + return 0;
  174. +}
  175. +
  176. +static inline int rt_i2c_wait_tx_done(void)
  177. +{
  178. + int i=0;
  179. + while((!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY)) && (i<i2c_busy_loop))
  180. + i++;
  181. + if(i>=i2c_busy_loop){
  182. + pr_err("err,wait for idle timeout");
  183. + return -ETIMEDOUT;
  184. + }
  185. + return 0;
  186. +}
  187. +
  188. +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
  189. +{
  190. + int i = 0, j = 0, pos = 0;
  191. + int nblock = msg->len / READ_BLOCK;
  192. + int rem = msg->len % READ_BLOCK;
  193. +
  194. + if (msg->flags & I2C_M_TEN) {
  195. + printk("10 bits addr not supported\n");
  196. + return -EINVAL;
  197. + }
  198. +
  199. + if (msg->flags & I2C_M_RD) {
  200. + for (i = 0; i < nblock; i++) {
  201. + if (rt_i2c_wait_idle())
  202. + goto err_timeout;
  203. + rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
  204. + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
  205. + for (j = 0; j < READ_BLOCK; j++) {
  206. + if (rt_i2c_wait_rx_done())
  207. + goto err_timeout;
  208. + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
  209. + }
  210. + }
  211. +
  212. + if (rt_i2c_wait_idle())
  213. + goto err_timeout;
  214. + rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
  215. + rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
  216. +
  217. + for (i = 0; i < rem; i++) {
  218. + if (rt_i2c_wait_rx_done())
  219. + goto err_timeout;
  220. + msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
  221. + }
  222. + } else {
  223. + if (rt_i2c_wait_idle())
  224. + goto err_timeout;
  225. + rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
  226. + for (i = 0; i < msg->len; i++) {
  227. + rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
  228. + if(i == 0)
  229. + rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
  230. +
  231. + if (rt_i2c_wait_tx_done())
  232. + goto err_timeout;
  233. + }
  234. + }
  235. +
  236. + return 0;
  237. +err_timeout:
  238. + return -ETIMEDOUT;
  239. +}
  240. +
  241. +static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
  242. +{
  243. + int i = 0;
  244. + int ret = 0;
  245. + i2c_master_init(a);
  246. + mt7621_i2c_enable(m);
  247. +
  248. + for (i = 0; i != n && ret==0; i++) {
  249. + ret = rt_i2c_handle_msg(a, &m[i]);
  250. + if (ret)
  251. + return ret;
  252. + }
  253. + return i;
  254. +}
  255. +
  256. +static u32 rt_i2c_func(struct i2c_adapter *a)
  257. +{
  258. + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  259. +}
  260. +
  261. +static const struct i2c_algorithm rt_i2c_algo = {
  262. + .master_xfer = rt_i2c_master_xfer,
  263. + .functionality = rt_i2c_func,
  264. +};
  265. +
  266. +static int rt_i2c_probe(struct platform_device *pdev)
  267. +{
  268. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. + int ret;
  270. +
  271. + adapter = devm_kzalloc(&pdev->dev,sizeof(struct i2c_adapter), GFP_KERNEL);
  272. + if (!adapter) {
  273. + dev_err(&pdev->dev, "failed to allocate i2c_adapter\n");
  274. + return -ENOMEM;
  275. + }
  276. + membase = devm_ioremap_resource(&pdev->dev, res);
  277. + if (IS_ERR(membase))
  278. + return PTR_ERR(membase);
  279. +
  280. + strlcpy(adapter->name, dev_name(&pdev->dev), sizeof(adapter->name));
  281. +
  282. + adapter->owner = THIS_MODULE;
  283. + adapter->nr = pdev->id;
  284. + adapter->timeout = HZ;
  285. + adapter->algo = &rt_i2c_algo;
  286. + adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  287. + adapter->dev.parent = &pdev->dev;
  288. + adapter->dev.of_node = pdev->dev.of_node;
  289. +
  290. + platform_set_drvdata(pdev, adapter);
  291. +
  292. + ret = i2c_add_numbered_adapter(adapter);
  293. + if (ret)
  294. + return ret;
  295. +
  296. + dev_info(&pdev->dev,"loaded");
  297. +
  298. + return 0;
  299. +}
  300. +
  301. +static int rt_i2c_remove(struct platform_device *pdev)
  302. +{
  303. + platform_set_drvdata(pdev, NULL);
  304. + return 0;
  305. +}
  306. +
  307. +static const struct of_device_id i2c_rt_dt_ids[] = {
  308. + { .compatible = "ralink,i2c-mt7621", },
  309. + { /* sentinel */ }
  310. +};
  311. +
  312. +MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
  313. +
  314. +static struct platform_driver rt_i2c_driver = {
  315. + .probe = rt_i2c_probe,
  316. + .remove = rt_i2c_remove,
  317. + .driver = {
  318. + .owner = THIS_MODULE,
  319. + .name = "i2c-mt7621",
  320. + .of_match_table = i2c_rt_dt_ids,
  321. + },
  322. +};
  323. +
  324. +static int __init i2c_rt_init (void)
  325. +{
  326. + return platform_driver_register(&rt_i2c_driver);
  327. +}
  328. +
  329. +static void __exit i2c_rt_exit (void)
  330. +{
  331. + platform_driver_unregister(&rt_i2c_driver);
  332. +}
  333. +module_init (i2c_rt_init);
  334. +module_exit (i2c_rt_exit);
  335. +
  336. +MODULE_AUTHOR("Steven Liu <[email protected]>");
  337. +MODULE_DESCRIPTION("MT7621 I2c host driver");
  338. +MODULE_LICENSE("GPL");
  339. +MODULE_ALIAS("platform:MT7621-I2C");