i2c-kempld.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * I2C bus driver for Kontron COM modules
  3. *
  4. * Copyright (c) 2010-2013 Kontron Europe GmbH
  5. * Author: Michael Brunner <michael.brunner@kontron.com>
  6. *
  7. * The driver is based on the i2c-ocores driver by Peter Korsgaard.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include <linux/mfd/kempld.h>
  23. #define KEMPLD_I2C_PRELOW 0x0b
  24. #define KEMPLD_I2C_PREHIGH 0x0c
  25. #define KEMPLD_I2C_DATA 0x0e
  26. #define KEMPLD_I2C_CTRL 0x0d
  27. #define I2C_CTRL_IEN 0x40
  28. #define I2C_CTRL_EN 0x80
  29. #define KEMPLD_I2C_STAT 0x0f
  30. #define I2C_STAT_IF 0x01
  31. #define I2C_STAT_TIP 0x02
  32. #define I2C_STAT_ARBLOST 0x20
  33. #define I2C_STAT_BUSY 0x40
  34. #define I2C_STAT_NACK 0x80
  35. #define KEMPLD_I2C_CMD 0x0f
  36. #define I2C_CMD_START 0x91
  37. #define I2C_CMD_STOP 0x41
  38. #define I2C_CMD_READ 0x21
  39. #define I2C_CMD_WRITE 0x11
  40. #define I2C_CMD_READ_ACK 0x21
  41. #define I2C_CMD_READ_NACK 0x29
  42. #define I2C_CMD_IACK 0x01
  43. #define KEMPLD_I2C_FREQ_MAX 2700 /* 2.7 mHz */
  44. #define KEMPLD_I2C_FREQ_STD 100 /* 100 kHz */
  45. enum {
  46. STATE_DONE = 0,
  47. STATE_INIT,
  48. STATE_ADDR,
  49. STATE_ADDR10,
  50. STATE_START,
  51. STATE_WRITE,
  52. STATE_READ,
  53. STATE_ERROR,
  54. };
  55. struct kempld_i2c_data {
  56. struct device *dev;
  57. struct kempld_device_data *pld;
  58. struct i2c_adapter adap;
  59. struct i2c_msg *msg;
  60. int pos;
  61. int nmsgs;
  62. int state;
  63. bool was_active;
  64. };
  65. static unsigned int bus_frequency = KEMPLD_I2C_FREQ_STD;
  66. module_param(bus_frequency, uint, 0);
  67. MODULE_PARM_DESC(bus_frequency, "Set I2C bus frequency in kHz (default="
  68. __MODULE_STRING(KEMPLD_I2C_FREQ_STD)")");
  69. static int i2c_bus = -1;
  70. module_param(i2c_bus, int, 0);
  71. MODULE_PARM_DESC(i2c_bus, "Set I2C bus number (default=-1 for dynamic assignment)");
  72. static bool i2c_gpio_mux;
  73. module_param(i2c_gpio_mux, bool, 0);
  74. MODULE_PARM_DESC(i2c_gpio_mux, "Enable I2C port on GPIO out (default=false)");
  75. /*
  76. * kempld_get_mutex must be called prior to calling this function.
  77. */
  78. static int kempld_i2c_process(struct kempld_i2c_data *i2c)
  79. {
  80. struct kempld_device_data *pld = i2c->pld;
  81. u8 stat = kempld_read8(pld, KEMPLD_I2C_STAT);
  82. struct i2c_msg *msg = i2c->msg;
  83. u8 addr;
  84. /* Ready? */
  85. if (stat & I2C_STAT_TIP)
  86. return -EBUSY;
  87. if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
  88. /* Stop has been sent */
  89. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_IACK);
  90. if (i2c->state == STATE_ERROR)
  91. return -EIO;
  92. return 0;
  93. }
  94. /* Error? */
  95. if (stat & I2C_STAT_ARBLOST) {
  96. i2c->state = STATE_ERROR;
  97. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_STOP);
  98. return -EAGAIN;
  99. }
  100. if (i2c->state == STATE_INIT) {
  101. if (stat & I2C_STAT_BUSY)
  102. return -EBUSY;
  103. i2c->state = STATE_ADDR;
  104. }
  105. if (i2c->state == STATE_ADDR) {
  106. /* 10 bit address? */
  107. if (i2c->msg->flags & I2C_M_TEN) {
  108. addr = 0xf0 | ((i2c->msg->addr >> 7) & 0x6);
  109. i2c->state = STATE_ADDR10;
  110. } else {
  111. addr = (i2c->msg->addr << 1);
  112. i2c->state = STATE_START;
  113. }
  114. /* Set read bit if necessary */
  115. addr |= (i2c->msg->flags & I2C_M_RD) ? 1 : 0;
  116. kempld_write8(pld, KEMPLD_I2C_DATA, addr);
  117. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_START);
  118. return 0;
  119. }
  120. /* Second part of 10 bit addressing */
  121. if (i2c->state == STATE_ADDR10) {
  122. kempld_write8(pld, KEMPLD_I2C_DATA, i2c->msg->addr & 0xff);
  123. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_WRITE);
  124. i2c->state = STATE_START;
  125. return 0;
  126. }
  127. if (i2c->state == STATE_START || i2c->state == STATE_WRITE) {
  128. i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  129. if (stat & I2C_STAT_NACK) {
  130. i2c->state = STATE_ERROR;
  131. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_STOP);
  132. return -ENXIO;
  133. }
  134. } else {
  135. msg->buf[i2c->pos++] = kempld_read8(pld, KEMPLD_I2C_DATA);
  136. }
  137. if (i2c->pos >= msg->len) {
  138. i2c->nmsgs--;
  139. i2c->msg++;
  140. i2c->pos = 0;
  141. msg = i2c->msg;
  142. if (i2c->nmsgs) {
  143. if (!(msg->flags & I2C_M_NOSTART)) {
  144. i2c->state = STATE_ADDR;
  145. return 0;
  146. } else {
  147. i2c->state = (msg->flags & I2C_M_RD)
  148. ? STATE_READ : STATE_WRITE;
  149. }
  150. } else {
  151. i2c->state = STATE_DONE;
  152. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_STOP);
  153. return 0;
  154. }
  155. }
  156. if (i2c->state == STATE_READ) {
  157. kempld_write8(pld, KEMPLD_I2C_CMD, i2c->pos == (msg->len - 1) ?
  158. I2C_CMD_READ_NACK : I2C_CMD_READ_ACK);
  159. } else {
  160. kempld_write8(pld, KEMPLD_I2C_DATA, msg->buf[i2c->pos++]);
  161. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_WRITE);
  162. }
  163. return 0;
  164. }
  165. static int kempld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  166. int num)
  167. {
  168. struct kempld_i2c_data *i2c = i2c_get_adapdata(adap);
  169. struct kempld_device_data *pld = i2c->pld;
  170. unsigned long timeout = jiffies + HZ;
  171. int ret;
  172. i2c->msg = msgs;
  173. i2c->pos = 0;
  174. i2c->nmsgs = num;
  175. i2c->state = STATE_INIT;
  176. /* Handle the transfer */
  177. while (time_before(jiffies, timeout)) {
  178. kempld_get_mutex(pld);
  179. ret = kempld_i2c_process(i2c);
  180. kempld_release_mutex(pld);
  181. if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR)
  182. return (i2c->state == STATE_DONE) ? num : ret;
  183. if (ret == 0)
  184. timeout = jiffies + HZ;
  185. usleep_range(5, 15);
  186. }
  187. i2c->state = STATE_ERROR;
  188. return -ETIMEDOUT;
  189. }
  190. /*
  191. * kempld_get_mutex must be called prior to calling this function.
  192. */
  193. static void kempld_i2c_device_init(struct kempld_i2c_data *i2c)
  194. {
  195. struct kempld_device_data *pld = i2c->pld;
  196. u16 prescale_corr;
  197. long prescale;
  198. u8 ctrl;
  199. u8 stat;
  200. u8 cfg;
  201. /* Make sure the device is disabled */
  202. ctrl = kempld_read8(pld, KEMPLD_I2C_CTRL);
  203. ctrl &= ~(I2C_CTRL_EN | I2C_CTRL_IEN);
  204. kempld_write8(pld, KEMPLD_I2C_CTRL, ctrl);
  205. if (bus_frequency > KEMPLD_I2C_FREQ_MAX)
  206. bus_frequency = KEMPLD_I2C_FREQ_MAX;
  207. if (pld->info.spec_major == 1)
  208. prescale = pld->pld_clock / (bus_frequency * 5) - 1000;
  209. else
  210. prescale = pld->pld_clock / (bus_frequency * 4) - 3000;
  211. if (prescale < 0)
  212. prescale = 0;
  213. /* Round to the best matching value */
  214. prescale_corr = prescale / 1000;
  215. if (prescale % 1000 >= 500)
  216. prescale_corr++;
  217. kempld_write8(pld, KEMPLD_I2C_PRELOW, prescale_corr & 0xff);
  218. kempld_write8(pld, KEMPLD_I2C_PREHIGH, prescale_corr >> 8);
  219. /* Activate I2C bus output on GPIO pins */
  220. cfg = kempld_read8(pld, KEMPLD_CFG);
  221. if (i2c_gpio_mux)
  222. cfg |= KEMPLD_CFG_GPIO_I2C_MUX;
  223. else
  224. cfg &= ~KEMPLD_CFG_GPIO_I2C_MUX;
  225. kempld_write8(pld, KEMPLD_CFG, cfg);
  226. /* Enable the device */
  227. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_IACK);
  228. ctrl |= I2C_CTRL_EN;
  229. kempld_write8(pld, KEMPLD_I2C_CTRL, ctrl);
  230. stat = kempld_read8(pld, KEMPLD_I2C_STAT);
  231. if (stat & I2C_STAT_BUSY)
  232. kempld_write8(pld, KEMPLD_I2C_CMD, I2C_CMD_STOP);
  233. }
  234. static u32 kempld_i2c_func(struct i2c_adapter *adap)
  235. {
  236. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
  237. }
  238. static const struct i2c_algorithm kempld_i2c_algorithm = {
  239. .master_xfer = kempld_i2c_xfer,
  240. .functionality = kempld_i2c_func,
  241. };
  242. static struct i2c_adapter kempld_i2c_adapter = {
  243. .owner = THIS_MODULE,
  244. .name = "i2c-kempld",
  245. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  246. .algo = &kempld_i2c_algorithm,
  247. };
  248. static int kempld_i2c_probe(struct platform_device *pdev)
  249. {
  250. struct kempld_device_data *pld = dev_get_drvdata(pdev->dev.parent);
  251. struct kempld_i2c_data *i2c;
  252. int ret;
  253. u8 ctrl;
  254. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  255. if (!i2c)
  256. return -ENOMEM;
  257. i2c->pld = pld;
  258. i2c->dev = &pdev->dev;
  259. i2c->adap = kempld_i2c_adapter;
  260. i2c->adap.dev.parent = i2c->dev;
  261. i2c_set_adapdata(&i2c->adap, i2c);
  262. platform_set_drvdata(pdev, i2c);
  263. kempld_get_mutex(pld);
  264. ctrl = kempld_read8(pld, KEMPLD_I2C_CTRL);
  265. if (ctrl & I2C_CTRL_EN)
  266. i2c->was_active = true;
  267. kempld_i2c_device_init(i2c);
  268. kempld_release_mutex(pld);
  269. /* Add I2C adapter to I2C tree */
  270. if (i2c_bus >= -1)
  271. i2c->adap.nr = i2c_bus;
  272. ret = i2c_add_numbered_adapter(&i2c->adap);
  273. if (ret)
  274. return ret;
  275. dev_info(i2c->dev, "I2C bus initialized at %dkHz\n",
  276. bus_frequency);
  277. return 0;
  278. }
  279. static int kempld_i2c_remove(struct platform_device *pdev)
  280. {
  281. struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
  282. struct kempld_device_data *pld = i2c->pld;
  283. u8 ctrl;
  284. kempld_get_mutex(pld);
  285. /*
  286. * Disable I2C logic if it was not activated before the
  287. * driver loaded
  288. */
  289. if (!i2c->was_active) {
  290. ctrl = kempld_read8(pld, KEMPLD_I2C_CTRL);
  291. ctrl &= ~I2C_CTRL_EN;
  292. kempld_write8(pld, KEMPLD_I2C_CTRL, ctrl);
  293. }
  294. kempld_release_mutex(pld);
  295. i2c_del_adapter(&i2c->adap);
  296. return 0;
  297. }
  298. #ifdef CONFIG_PM
  299. static int kempld_i2c_suspend(struct platform_device *pdev, pm_message_t state)
  300. {
  301. struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
  302. struct kempld_device_data *pld = i2c->pld;
  303. u8 ctrl;
  304. kempld_get_mutex(pld);
  305. ctrl = kempld_read8(pld, KEMPLD_I2C_CTRL);
  306. ctrl &= ~I2C_CTRL_EN;
  307. kempld_write8(pld, KEMPLD_I2C_CTRL, ctrl);
  308. kempld_release_mutex(pld);
  309. return 0;
  310. }
  311. static int kempld_i2c_resume(struct platform_device *pdev)
  312. {
  313. struct kempld_i2c_data *i2c = platform_get_drvdata(pdev);
  314. struct kempld_device_data *pld = i2c->pld;
  315. kempld_get_mutex(pld);
  316. kempld_i2c_device_init(i2c);
  317. kempld_release_mutex(pld);
  318. return 0;
  319. }
  320. #else
  321. #define kempld_i2c_suspend NULL
  322. #define kempld_i2c_resume NULL
  323. #endif
  324. static struct platform_driver kempld_i2c_driver = {
  325. .driver = {
  326. .name = "kempld-i2c",
  327. },
  328. .probe = kempld_i2c_probe,
  329. .remove = kempld_i2c_remove,
  330. .suspend = kempld_i2c_suspend,
  331. .resume = kempld_i2c_resume,
  332. };
  333. module_platform_driver(kempld_i2c_driver);
  334. MODULE_DESCRIPTION("KEM PLD I2C Driver");
  335. MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:kempld_i2c");