bbc_i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
  2. * platforms.
  3. *
  4. * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/wait.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <asm/bbc.h>
  17. #include <asm/io.h>
  18. #include "bbc_i2c.h"
  19. /* Convert this driver to use i2c bus layer someday... */
  20. #define I2C_PCF_PIN 0x80
  21. #define I2C_PCF_ESO 0x40
  22. #define I2C_PCF_ES1 0x20
  23. #define I2C_PCF_ES2 0x10
  24. #define I2C_PCF_ENI 0x08
  25. #define I2C_PCF_STA 0x04
  26. #define I2C_PCF_STO 0x02
  27. #define I2C_PCF_ACK 0x01
  28. #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
  29. #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
  30. #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
  31. #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
  32. #define I2C_PCF_INI 0x40 /* 1 if not initialized */
  33. #define I2C_PCF_STS 0x20
  34. #define I2C_PCF_BER 0x10
  35. #define I2C_PCF_AD0 0x08
  36. #define I2C_PCF_LRB 0x08
  37. #define I2C_PCF_AAS 0x04
  38. #define I2C_PCF_LAB 0x02
  39. #define I2C_PCF_BB 0x01
  40. /* The BBC devices have two I2C controllers. The first I2C controller
  41. * connects mainly to configuration proms (NVRAM, cpu configuration,
  42. * dimm types, etc.). Whereas the second I2C controller connects to
  43. * environmental control devices such as fans and temperature sensors.
  44. * The second controller also connects to the smartcard reader, if present.
  45. */
  46. static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val)
  47. {
  48. int i;
  49. for (i = 0; i < NUM_CHILDREN; i++) {
  50. if (bp->devs[i].device == op) {
  51. bp->devs[i].client_claimed = val;
  52. return;
  53. }
  54. }
  55. }
  56. #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
  57. #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
  58. struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
  59. {
  60. struct platform_device *op = NULL;
  61. int curidx = 0, i;
  62. for (i = 0; i < NUM_CHILDREN; i++) {
  63. if (!(op = bp->devs[i].device))
  64. break;
  65. if (curidx == index)
  66. goto out;
  67. op = NULL;
  68. curidx++;
  69. }
  70. out:
  71. if (curidx == index)
  72. return op;
  73. return NULL;
  74. }
  75. struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op)
  76. {
  77. struct bbc_i2c_client *client;
  78. const u32 *reg;
  79. client = kzalloc(sizeof(*client), GFP_KERNEL);
  80. if (!client)
  81. return NULL;
  82. client->bp = bp;
  83. client->op = op;
  84. reg = of_get_property(op->dev.of_node, "reg", NULL);
  85. if (!reg) {
  86. kfree(client);
  87. return NULL;
  88. }
  89. client->bus = reg[0];
  90. client->address = reg[1];
  91. claim_device(bp, op);
  92. return client;
  93. }
  94. void bbc_i2c_detach(struct bbc_i2c_client *client)
  95. {
  96. struct bbc_i2c_bus *bp = client->bp;
  97. struct platform_device *op = client->op;
  98. release_device(bp, op);
  99. kfree(client);
  100. }
  101. static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
  102. {
  103. DECLARE_WAITQUEUE(wait, current);
  104. int limit = 32;
  105. int ret = 1;
  106. bp->waiting = 1;
  107. add_wait_queue(&bp->wq, &wait);
  108. while (limit-- > 0) {
  109. long val;
  110. val = wait_event_interruptible_timeout(
  111. bp->wq,
  112. (((*status = readb(bp->i2c_control_regs + 0))
  113. & I2C_PCF_PIN) == 0),
  114. msecs_to_jiffies(250));
  115. if (val > 0) {
  116. ret = 0;
  117. break;
  118. }
  119. }
  120. remove_wait_queue(&bp->wq, &wait);
  121. bp->waiting = 0;
  122. return ret;
  123. }
  124. int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
  125. {
  126. struct bbc_i2c_bus *bp = client->bp;
  127. int address = client->address;
  128. u8 status;
  129. int ret = -1;
  130. if (bp->i2c_bussel_reg != NULL)
  131. writeb(client->bus, bp->i2c_bussel_reg);
  132. writeb(address, bp->i2c_control_regs + 0x1);
  133. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  134. if (wait_for_pin(bp, &status))
  135. goto out;
  136. writeb(off, bp->i2c_control_regs + 0x1);
  137. if (wait_for_pin(bp, &status) ||
  138. (status & I2C_PCF_LRB) != 0)
  139. goto out;
  140. writeb(val, bp->i2c_control_regs + 0x1);
  141. if (wait_for_pin(bp, &status))
  142. goto out;
  143. ret = 0;
  144. out:
  145. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  146. return ret;
  147. }
  148. int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
  149. {
  150. struct bbc_i2c_bus *bp = client->bp;
  151. unsigned char address = client->address, status;
  152. int ret = -1;
  153. if (bp->i2c_bussel_reg != NULL)
  154. writeb(client->bus, bp->i2c_bussel_reg);
  155. writeb(address, bp->i2c_control_regs + 0x1);
  156. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  157. if (wait_for_pin(bp, &status))
  158. goto out;
  159. writeb(off, bp->i2c_control_regs + 0x1);
  160. if (wait_for_pin(bp, &status) ||
  161. (status & I2C_PCF_LRB) != 0)
  162. goto out;
  163. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  164. address |= 0x1; /* READ */
  165. writeb(address, bp->i2c_control_regs + 0x1);
  166. writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
  167. if (wait_for_pin(bp, &status))
  168. goto out;
  169. /* Set PIN back to one so the device sends the first
  170. * byte.
  171. */
  172. (void) readb(bp->i2c_control_regs + 0x1);
  173. if (wait_for_pin(bp, &status))
  174. goto out;
  175. writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
  176. *byte = readb(bp->i2c_control_regs + 0x1);
  177. if (wait_for_pin(bp, &status))
  178. goto out;
  179. ret = 0;
  180. out:
  181. writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
  182. (void) readb(bp->i2c_control_regs + 0x1);
  183. return ret;
  184. }
  185. int bbc_i2c_write_buf(struct bbc_i2c_client *client,
  186. char *buf, int len, int off)
  187. {
  188. int ret = 0;
  189. while (len > 0) {
  190. ret = bbc_i2c_writeb(client, *buf, off);
  191. if (ret < 0)
  192. break;
  193. len--;
  194. buf++;
  195. off++;
  196. }
  197. return ret;
  198. }
  199. int bbc_i2c_read_buf(struct bbc_i2c_client *client,
  200. char *buf, int len, int off)
  201. {
  202. int ret = 0;
  203. while (len > 0) {
  204. ret = bbc_i2c_readb(client, buf, off);
  205. if (ret < 0)
  206. break;
  207. len--;
  208. buf++;
  209. off++;
  210. }
  211. return ret;
  212. }
  213. EXPORT_SYMBOL(bbc_i2c_getdev);
  214. EXPORT_SYMBOL(bbc_i2c_attach);
  215. EXPORT_SYMBOL(bbc_i2c_detach);
  216. EXPORT_SYMBOL(bbc_i2c_writeb);
  217. EXPORT_SYMBOL(bbc_i2c_readb);
  218. EXPORT_SYMBOL(bbc_i2c_write_buf);
  219. EXPORT_SYMBOL(bbc_i2c_read_buf);
  220. static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
  221. {
  222. struct bbc_i2c_bus *bp = dev_id;
  223. /* PIN going from set to clear is the only event which
  224. * makes the i2c assert an interrupt.
  225. */
  226. if (bp->waiting &&
  227. !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
  228. wake_up_interruptible(&bp->wq);
  229. return IRQ_HANDLED;
  230. }
  231. static void reset_one_i2c(struct bbc_i2c_bus *bp)
  232. {
  233. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  234. writeb(bp->own, bp->i2c_control_regs + 0x1);
  235. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  236. writeb(bp->clock, bp->i2c_control_regs + 0x1);
  237. writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
  238. }
  239. static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index)
  240. {
  241. struct bbc_i2c_bus *bp;
  242. struct device_node *dp;
  243. int entry;
  244. bp = kzalloc(sizeof(*bp), GFP_KERNEL);
  245. if (!bp)
  246. return NULL;
  247. INIT_LIST_HEAD(&bp->temps);
  248. INIT_LIST_HEAD(&bp->fans);
  249. bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
  250. if (!bp->i2c_control_regs)
  251. goto fail;
  252. if (op->num_resources == 2) {
  253. bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
  254. if (!bp->i2c_bussel_reg)
  255. goto fail;
  256. }
  257. bp->waiting = 0;
  258. init_waitqueue_head(&bp->wq);
  259. if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt,
  260. IRQF_SHARED, "bbc_i2c", bp))
  261. goto fail;
  262. bp->index = index;
  263. bp->op = op;
  264. spin_lock_init(&bp->lock);
  265. entry = 0;
  266. for (dp = op->dev.of_node->child;
  267. dp && entry < 8;
  268. dp = dp->sibling, entry++) {
  269. struct platform_device *child_op;
  270. child_op = of_find_device_by_node(dp);
  271. bp->devs[entry].device = child_op;
  272. bp->devs[entry].client_claimed = 0;
  273. }
  274. writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
  275. bp->own = readb(bp->i2c_control_regs + 0x01);
  276. writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
  277. bp->clock = readb(bp->i2c_control_regs + 0x01);
  278. printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
  279. bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
  280. reset_one_i2c(bp);
  281. return bp;
  282. fail:
  283. if (bp->i2c_bussel_reg)
  284. of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
  285. if (bp->i2c_control_regs)
  286. of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
  287. kfree(bp);
  288. return NULL;
  289. }
  290. extern int bbc_envctrl_init(struct bbc_i2c_bus *bp);
  291. extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp);
  292. static int bbc_i2c_probe(struct platform_device *op)
  293. {
  294. struct bbc_i2c_bus *bp;
  295. int err, index = 0;
  296. bp = attach_one_i2c(op, index);
  297. if (!bp)
  298. return -EINVAL;
  299. err = bbc_envctrl_init(bp);
  300. if (err) {
  301. free_irq(op->archdata.irqs[0], bp);
  302. if (bp->i2c_bussel_reg)
  303. of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
  304. if (bp->i2c_control_regs)
  305. of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
  306. kfree(bp);
  307. } else {
  308. dev_set_drvdata(&op->dev, bp);
  309. }
  310. return err;
  311. }
  312. static int bbc_i2c_remove(struct platform_device *op)
  313. {
  314. struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);
  315. bbc_envctrl_cleanup(bp);
  316. free_irq(op->archdata.irqs[0], bp);
  317. if (bp->i2c_bussel_reg)
  318. of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
  319. if (bp->i2c_control_regs)
  320. of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
  321. kfree(bp);
  322. return 0;
  323. }
  324. static const struct of_device_id bbc_i2c_match[] = {
  325. {
  326. .name = "i2c",
  327. .compatible = "SUNW,bbc-i2c",
  328. },
  329. {},
  330. };
  331. MODULE_DEVICE_TABLE(of, bbc_i2c_match);
  332. static struct platform_driver bbc_i2c_driver = {
  333. .driver = {
  334. .name = "bbc_i2c",
  335. .of_match_table = bbc_i2c_match,
  336. },
  337. .probe = bbc_i2c_probe,
  338. .remove = bbc_i2c_remove,
  339. };
  340. module_platform_driver(bbc_i2c_driver);
  341. MODULE_LICENSE("GPL");