0111-i2c-MIPS-add-mt7621-I2C-driver.patch 8.7 KB

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