i2c-mpc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
  4. * This is a combined i2c adapter and algorithm driver for the
  5. * MPC107/Tsi107 PowerPC northbridge and processors that include
  6. * the same I2C unit (8240, 8245, 85xx).
  7. *
  8. * Release 0.8
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <linux/fsl_devices.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <asm/mpc52xx.h>
  27. #include <sysdev/fsl_soc.h>
  28. #define DRV_NAME "mpc-i2c"
  29. #define MPC_I2C_CLOCK_LEGACY 0
  30. #define MPC_I2C_CLOCK_PRESERVE (~0U)
  31. #define MPC_I2C_FDR 0x04
  32. #define MPC_I2C_CR 0x08
  33. #define MPC_I2C_SR 0x0c
  34. #define MPC_I2C_DR 0x10
  35. #define MPC_I2C_DFSRR 0x14
  36. #define CCR_MEN 0x80
  37. #define CCR_MIEN 0x40
  38. #define CCR_MSTA 0x20
  39. #define CCR_MTX 0x10
  40. #define CCR_TXAK 0x08
  41. #define CCR_RSTA 0x04
  42. #define CSR_MCF 0x80
  43. #define CSR_MAAS 0x40
  44. #define CSR_MBB 0x20
  45. #define CSR_MAL 0x10
  46. #define CSR_SRW 0x04
  47. #define CSR_MIF 0x02
  48. #define CSR_RXAK 0x01
  49. struct mpc_i2c {
  50. struct device *dev;
  51. void __iomem *base;
  52. u32 interrupt;
  53. wait_queue_head_t queue;
  54. struct i2c_adapter adap;
  55. int irq;
  56. u32 real_clk;
  57. };
  58. struct mpc_i2c_divider {
  59. u16 divider;
  60. u16 fdr; /* including dfsrr */
  61. };
  62. struct mpc_i2c_data {
  63. void (*setup)(struct device_node *node, struct mpc_i2c *i2c,
  64. u32 clock, u32 prescaler);
  65. u32 prescaler;
  66. };
  67. static inline void writeccr(struct mpc_i2c *i2c, u32 x)
  68. {
  69. writeb(x, i2c->base + MPC_I2C_CR);
  70. }
  71. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
  72. {
  73. struct mpc_i2c *i2c = dev_id;
  74. if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
  75. /* Read again to allow register to stabilise */
  76. i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
  77. writeb(0, i2c->base + MPC_I2C_SR);
  78. wake_up(&i2c->queue);
  79. }
  80. return IRQ_HANDLED;
  81. }
  82. /* Sometimes 9th clock pulse isn't generated, and slave doesn't release
  83. * the bus, because it wants to send ACK.
  84. * Following sequence of enabling/disabling and sending start/stop generates
  85. * the 9 pulses, so it's all OK.
  86. */
  87. static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  88. {
  89. int k;
  90. u32 delay_val = 1000000 / i2c->real_clk + 1;
  91. if (delay_val < 2)
  92. delay_val = 2;
  93. for (k = 9; k; k--) {
  94. writeccr(i2c, 0);
  95. writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
  96. udelay(delay_val);
  97. writeccr(i2c, CCR_MEN);
  98. udelay(delay_val << 1);
  99. }
  100. }
  101. static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
  102. {
  103. unsigned long orig_jiffies = jiffies;
  104. u32 x;
  105. int result = 0;
  106. if (!i2c->irq) {
  107. while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
  108. schedule();
  109. if (time_after(jiffies, orig_jiffies + timeout)) {
  110. dev_dbg(i2c->dev, "timeout\n");
  111. writeccr(i2c, 0);
  112. result = -EIO;
  113. break;
  114. }
  115. }
  116. x = readb(i2c->base + MPC_I2C_SR);
  117. writeb(0, i2c->base + MPC_I2C_SR);
  118. } else {
  119. /* Interrupt mode */
  120. result = wait_event_timeout(i2c->queue,
  121. (i2c->interrupt & CSR_MIF), timeout);
  122. if (unlikely(!(i2c->interrupt & CSR_MIF))) {
  123. dev_dbg(i2c->dev, "wait timeout\n");
  124. writeccr(i2c, 0);
  125. result = -ETIMEDOUT;
  126. }
  127. x = i2c->interrupt;
  128. i2c->interrupt = 0;
  129. }
  130. if (result < 0)
  131. return result;
  132. if (!(x & CSR_MCF)) {
  133. dev_dbg(i2c->dev, "unfinished\n");
  134. return -EIO;
  135. }
  136. if (x & CSR_MAL) {
  137. dev_dbg(i2c->dev, "MAL\n");
  138. return -EIO;
  139. }
  140. if (writing && (x & CSR_RXAK)) {
  141. dev_dbg(i2c->dev, "No RXAK\n");
  142. /* generate stop */
  143. writeccr(i2c, CCR_MEN);
  144. return -EIO;
  145. }
  146. return 0;
  147. }
  148. #if defined(CONFIG_PPC_MPC52xx) || defined(CONFIG_PPC_MPC512x)
  149. static const struct mpc_i2c_divider mpc_i2c_dividers_52xx[] __devinitconst = {
  150. {20, 0x20}, {22, 0x21}, {24, 0x22}, {26, 0x23},
  151. {28, 0x24}, {30, 0x01}, {32, 0x25}, {34, 0x02},
  152. {36, 0x26}, {40, 0x27}, {44, 0x04}, {48, 0x28},
  153. {52, 0x63}, {56, 0x29}, {60, 0x41}, {64, 0x2a},
  154. {68, 0x07}, {72, 0x2b}, {80, 0x2c}, {88, 0x09},
  155. {96, 0x2d}, {104, 0x0a}, {112, 0x2e}, {120, 0x81},
  156. {128, 0x2f}, {136, 0x47}, {144, 0x0c}, {160, 0x30},
  157. {176, 0x49}, {192, 0x31}, {208, 0x4a}, {224, 0x32},
  158. {240, 0x0f}, {256, 0x33}, {272, 0x87}, {288, 0x10},
  159. {320, 0x34}, {352, 0x89}, {384, 0x35}, {416, 0x8a},
  160. {448, 0x36}, {480, 0x13}, {512, 0x37}, {576, 0x14},
  161. {640, 0x38}, {768, 0x39}, {896, 0x3a}, {960, 0x17},
  162. {1024, 0x3b}, {1152, 0x18}, {1280, 0x3c}, {1536, 0x3d},
  163. {1792, 0x3e}, {1920, 0x1b}, {2048, 0x3f}, {2304, 0x1c},
  164. {2560, 0x1d}, {3072, 0x1e}, {3584, 0x7e}, {3840, 0x1f},
  165. {4096, 0x7f}, {4608, 0x5c}, {5120, 0x5d}, {6144, 0x5e},
  166. {7168, 0xbe}, {7680, 0x5f}, {8192, 0xbf}, {9216, 0x9c},
  167. {10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
  168. };
  169. static int __devinit mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock,
  170. int prescaler, u32 *real_clk)
  171. {
  172. const struct mpc_i2c_divider *div = NULL;
  173. unsigned int pvr = mfspr(SPRN_PVR);
  174. u32 divider;
  175. int i;
  176. if (clock == MPC_I2C_CLOCK_LEGACY) {
  177. /* see below - default fdr = 0x3f -> div = 2048 */
  178. *real_clk = mpc5xxx_get_bus_frequency(node) / 2048;
  179. return -EINVAL;
  180. }
  181. /* Determine divider value */
  182. divider = mpc5xxx_get_bus_frequency(node) / clock;
  183. /*
  184. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  185. * is equal to or lower than the requested speed.
  186. */
  187. for (i = 0; i < ARRAY_SIZE(mpc_i2c_dividers_52xx); i++) {
  188. div = &mpc_i2c_dividers_52xx[i];
  189. /* Old MPC5200 rev A CPUs do not support the high bits */
  190. if (div->fdr & 0xc0 && pvr == 0x80822011)
  191. continue;
  192. if (div->divider >= divider)
  193. break;
  194. }
  195. *real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
  196. return (int)div->fdr;
  197. }
  198. static void __devinit mpc_i2c_setup_52xx(struct device_node *node,
  199. struct mpc_i2c *i2c,
  200. u32 clock, u32 prescaler)
  201. {
  202. int ret, fdr;
  203. if (clock == MPC_I2C_CLOCK_PRESERVE) {
  204. dev_dbg(i2c->dev, "using fdr %d\n",
  205. readb(i2c->base + MPC_I2C_FDR));
  206. return;
  207. }
  208. ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->real_clk);
  209. fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */
  210. writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
  211. if (ret >= 0)
  212. dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
  213. fdr);
  214. }
  215. #else /* !(CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x) */
  216. static void __devinit mpc_i2c_setup_52xx(struct device_node *node,
  217. struct mpc_i2c *i2c,
  218. u32 clock, u32 prescaler)
  219. {
  220. }
  221. #endif /* CONFIG_PPC_MPC52xx || CONFIG_PPC_MPC512x */
  222. #ifdef CONFIG_PPC_MPC512x
  223. static void __devinit mpc_i2c_setup_512x(struct device_node *node,
  224. struct mpc_i2c *i2c,
  225. u32 clock, u32 prescaler)
  226. {
  227. struct device_node *node_ctrl;
  228. void __iomem *ctrl;
  229. const u32 *pval;
  230. u32 idx;
  231. /* Enable I2C interrupts for mpc5121 */
  232. node_ctrl = of_find_compatible_node(NULL, NULL,
  233. "fsl,mpc5121-i2c-ctrl");
  234. if (node_ctrl) {
  235. ctrl = of_iomap(node_ctrl, 0);
  236. if (ctrl) {
  237. /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */
  238. pval = of_get_property(node, "reg", NULL);
  239. idx = (*pval & 0xff) / 0x20;
  240. setbits32(ctrl, 1 << (24 + idx * 2));
  241. iounmap(ctrl);
  242. }
  243. of_node_put(node_ctrl);
  244. }
  245. /* The clock setup for the 52xx works also fine for the 512x */
  246. mpc_i2c_setup_52xx(node, i2c, clock, prescaler);
  247. }
  248. #else /* CONFIG_PPC_MPC512x */
  249. static void __devinit mpc_i2c_setup_512x(struct device_node *node,
  250. struct mpc_i2c *i2c,
  251. u32 clock, u32 prescaler)
  252. {
  253. }
  254. #endif /* CONFIG_PPC_MPC512x */
  255. #ifdef CONFIG_FSL_SOC
  256. static const struct mpc_i2c_divider mpc_i2c_dividers_8xxx[] __devinitconst = {
  257. {160, 0x0120}, {192, 0x0121}, {224, 0x0122}, {256, 0x0123},
  258. {288, 0x0100}, {320, 0x0101}, {352, 0x0601}, {384, 0x0102},
  259. {416, 0x0602}, {448, 0x0126}, {480, 0x0103}, {512, 0x0127},
  260. {544, 0x0b03}, {576, 0x0104}, {608, 0x1603}, {640, 0x0105},
  261. {672, 0x2003}, {704, 0x0b05}, {736, 0x2b03}, {768, 0x0106},
  262. {800, 0x3603}, {832, 0x0b06}, {896, 0x012a}, {960, 0x0107},
  263. {1024, 0x012b}, {1088, 0x1607}, {1152, 0x0108}, {1216, 0x2b07},
  264. {1280, 0x0109}, {1408, 0x1609}, {1536, 0x010a}, {1664, 0x160a},
  265. {1792, 0x012e}, {1920, 0x010b}, {2048, 0x012f}, {2176, 0x2b0b},
  266. {2304, 0x010c}, {2560, 0x010d}, {2816, 0x2b0d}, {3072, 0x010e},
  267. {3328, 0x2b0e}, {3584, 0x0132}, {3840, 0x010f}, {4096, 0x0133},
  268. {4608, 0x0110}, {5120, 0x0111}, {6144, 0x0112}, {7168, 0x0136},
  269. {7680, 0x0113}, {8192, 0x0137}, {9216, 0x0114}, {10240, 0x0115},
  270. {12288, 0x0116}, {14336, 0x013a}, {15360, 0x0117}, {16384, 0x013b},
  271. {18432, 0x0118}, {20480, 0x0119}, {24576, 0x011a}, {28672, 0x013e},
  272. {30720, 0x011b}, {32768, 0x013f}, {36864, 0x011c}, {40960, 0x011d},
  273. {49152, 0x011e}, {61440, 0x011f}
  274. };
  275. static u32 __devinit mpc_i2c_get_sec_cfg_8xxx(void)
  276. {
  277. struct device_node *node = NULL;
  278. u32 __iomem *reg;
  279. u32 val = 0;
  280. node = of_find_node_by_name(NULL, "global-utilities");
  281. if (node) {
  282. const u32 *prop = of_get_property(node, "reg", NULL);
  283. if (prop) {
  284. /*
  285. * Map and check POR Device Status Register 2
  286. * (PORDEVSR2) at 0xE0014
  287. */
  288. reg = ioremap(get_immrbase() + *prop + 0x14, 0x4);
  289. if (!reg)
  290. printk(KERN_ERR
  291. "Error: couldn't map PORDEVSR2\n");
  292. else
  293. val = in_be32(reg) & 0x00000080; /* sec-cfg */
  294. iounmap(reg);
  295. }
  296. }
  297. if (node)
  298. of_node_put(node);
  299. return val;
  300. }
  301. static int __devinit mpc_i2c_get_fdr_8xxx(struct device_node *node, u32 clock,
  302. u32 prescaler, u32 *real_clk)
  303. {
  304. const struct mpc_i2c_divider *div = NULL;
  305. u32 divider;
  306. int i;
  307. if (clock == MPC_I2C_CLOCK_LEGACY) {
  308. /* see below - default fdr = 0x1031 -> div = 16 * 3072 */
  309. *real_clk = fsl_get_sys_freq() / prescaler / (16 * 3072);
  310. return -EINVAL;
  311. }
  312. /* Determine proper divider value */
  313. if (of_device_is_compatible(node, "fsl,mpc8544-i2c"))
  314. prescaler = mpc_i2c_get_sec_cfg_8xxx() ? 3 : 2;
  315. if (!prescaler)
  316. prescaler = 1;
  317. divider = fsl_get_sys_freq() / clock / prescaler;
  318. pr_debug("I2C: src_clock=%d clock=%d divider=%d\n",
  319. fsl_get_sys_freq(), clock, divider);
  320. /*
  321. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  322. * is equal to or lower than the requested speed.
  323. */
  324. for (i = 0; i < ARRAY_SIZE(mpc_i2c_dividers_8xxx); i++) {
  325. div = &mpc_i2c_dividers_8xxx[i];
  326. if (div->divider >= divider)
  327. break;
  328. }
  329. *real_clk = fsl_get_sys_freq() / prescaler / div->divider;
  330. return div ? (int)div->fdr : -EINVAL;
  331. }
  332. static void __devinit mpc_i2c_setup_8xxx(struct device_node *node,
  333. struct mpc_i2c *i2c,
  334. u32 clock, u32 prescaler)
  335. {
  336. int ret, fdr;
  337. if (clock == MPC_I2C_CLOCK_PRESERVE) {
  338. dev_dbg(i2c->dev, "using dfsrr %d, fdr %d\n",
  339. readb(i2c->base + MPC_I2C_DFSRR),
  340. readb(i2c->base + MPC_I2C_FDR));
  341. return;
  342. }
  343. ret = mpc_i2c_get_fdr_8xxx(node, clock, prescaler, &i2c->real_clk);
  344. fdr = (ret >= 0) ? ret : 0x1031; /* backward compatibility */
  345. writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
  346. writeb((fdr >> 8) & 0xff, i2c->base + MPC_I2C_DFSRR);
  347. if (ret >= 0)
  348. dev_info(i2c->dev, "clock %d Hz (dfsrr=%d fdr=%d)\n",
  349. i2c->real_clk, fdr >> 8, fdr & 0xff);
  350. }
  351. #else /* !CONFIG_FSL_SOC */
  352. static void __devinit mpc_i2c_setup_8xxx(struct device_node *node,
  353. struct mpc_i2c *i2c,
  354. u32 clock, u32 prescaler)
  355. {
  356. }
  357. #endif /* CONFIG_FSL_SOC */
  358. static void mpc_i2c_start(struct mpc_i2c *i2c)
  359. {
  360. /* Clear arbitration */
  361. writeb(0, i2c->base + MPC_I2C_SR);
  362. /* Start with MEN */
  363. writeccr(i2c, CCR_MEN);
  364. }
  365. static void mpc_i2c_stop(struct mpc_i2c *i2c)
  366. {
  367. writeccr(i2c, CCR_MEN);
  368. }
  369. static int mpc_write(struct mpc_i2c *i2c, int target,
  370. const u8 *data, int length, int restart)
  371. {
  372. int i, result;
  373. unsigned timeout = i2c->adap.timeout;
  374. u32 flags = restart ? CCR_RSTA : 0;
  375. /* Start as master */
  376. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  377. /* Write target byte */
  378. writeb((target << 1), i2c->base + MPC_I2C_DR);
  379. result = i2c_wait(i2c, timeout, 1);
  380. if (result < 0)
  381. return result;
  382. for (i = 0; i < length; i++) {
  383. /* Write data byte */
  384. writeb(data[i], i2c->base + MPC_I2C_DR);
  385. result = i2c_wait(i2c, timeout, 1);
  386. if (result < 0)
  387. return result;
  388. }
  389. return 0;
  390. }
  391. static int mpc_read(struct mpc_i2c *i2c, int target,
  392. u8 *data, int length, int restart)
  393. {
  394. unsigned timeout = i2c->adap.timeout;
  395. int i, result;
  396. u32 flags = restart ? CCR_RSTA : 0;
  397. /* Switch to read - restart */
  398. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  399. /* Write target address byte - this time with the read flag set */
  400. writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
  401. result = i2c_wait(i2c, timeout, 1);
  402. if (result < 0)
  403. return result;
  404. if (length) {
  405. if (length == 1)
  406. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  407. else
  408. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
  409. /* Dummy read */
  410. readb(i2c->base + MPC_I2C_DR);
  411. }
  412. for (i = 0; i < length; i++) {
  413. result = i2c_wait(i2c, timeout, 0);
  414. if (result < 0)
  415. return result;
  416. /* Generate txack on next to last byte */
  417. if (i == length - 2)
  418. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  419. /* Do not generate stop on last byte */
  420. if (i == length - 1)
  421. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX);
  422. data[i] = readb(i2c->base + MPC_I2C_DR);
  423. }
  424. return length;
  425. }
  426. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  427. {
  428. struct i2c_msg *pmsg;
  429. int i;
  430. int ret = 0;
  431. unsigned long orig_jiffies = jiffies;
  432. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  433. mpc_i2c_start(i2c);
  434. /* Allow bus up to 1s to become not busy */
  435. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  436. if (signal_pending(current)) {
  437. dev_dbg(i2c->dev, "Interrupted\n");
  438. writeccr(i2c, 0);
  439. return -EINTR;
  440. }
  441. if (time_after(jiffies, orig_jiffies + HZ)) {
  442. u8 status = readb(i2c->base + MPC_I2C_SR);
  443. dev_dbg(i2c->dev, "timeout\n");
  444. if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
  445. writeb(status & ~CSR_MAL,
  446. i2c->base + MPC_I2C_SR);
  447. mpc_i2c_fixup(i2c);
  448. }
  449. return -EIO;
  450. }
  451. schedule();
  452. }
  453. for (i = 0; ret >= 0 && i < num; i++) {
  454. pmsg = &msgs[i];
  455. dev_dbg(i2c->dev,
  456. "Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  457. pmsg->flags & I2C_M_RD ? "read" : "write",
  458. pmsg->len, pmsg->addr, i + 1, num);
  459. if (pmsg->flags & I2C_M_RD)
  460. ret =
  461. mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  462. else
  463. ret =
  464. mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  465. }
  466. mpc_i2c_stop(i2c);
  467. return (ret < 0) ? ret : num;
  468. }
  469. static u32 mpc_functionality(struct i2c_adapter *adap)
  470. {
  471. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  472. }
  473. static const struct i2c_algorithm mpc_algo = {
  474. .master_xfer = mpc_xfer,
  475. .functionality = mpc_functionality,
  476. };
  477. static struct i2c_adapter mpc_ops = {
  478. .owner = THIS_MODULE,
  479. .name = "MPC adapter",
  480. .algo = &mpc_algo,
  481. .timeout = HZ,
  482. };
  483. static int __devinit fsl_i2c_probe(struct platform_device *op)
  484. {
  485. struct mpc_i2c *i2c;
  486. const u32 *prop;
  487. u32 clock = MPC_I2C_CLOCK_LEGACY;
  488. int result = 0;
  489. int plen;
  490. if (!op->dev.of_match)
  491. return -EINVAL;
  492. i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
  493. if (!i2c)
  494. return -ENOMEM;
  495. i2c->dev = &op->dev; /* for debug and error output */
  496. init_waitqueue_head(&i2c->queue);
  497. i2c->base = of_iomap(op->dev.of_node, 0);
  498. if (!i2c->base) {
  499. dev_err(i2c->dev, "failed to map controller\n");
  500. result = -ENOMEM;
  501. goto fail_map;
  502. }
  503. i2c->irq = irq_of_parse_and_map(op->dev.of_node, 0);
  504. if (i2c->irq) { /* no i2c->irq implies polling */
  505. result = request_irq(i2c->irq, mpc_i2c_isr,
  506. IRQF_SHARED, "i2c-mpc", i2c);
  507. if (result < 0) {
  508. dev_err(i2c->dev, "failed to attach interrupt\n");
  509. goto fail_request;
  510. }
  511. }
  512. if (of_get_property(op->dev.of_node, "fsl,preserve-clocking", NULL)) {
  513. clock = MPC_I2C_CLOCK_PRESERVE;
  514. } else {
  515. prop = of_get_property(op->dev.of_node, "clock-frequency",
  516. &plen);
  517. if (prop && plen == sizeof(u32))
  518. clock = *prop;
  519. }
  520. if (op->dev.of_match->data) {
  521. struct mpc_i2c_data *data = op->dev.of_match->data;
  522. data->setup(op->dev.of_node, i2c, clock, data->prescaler);
  523. } else {
  524. /* Backwards compatibility */
  525. if (of_get_property(op->dev.of_node, "dfsrr", NULL))
  526. mpc_i2c_setup_8xxx(op->dev.of_node, i2c, clock, 0);
  527. }
  528. prop = of_get_property(op->dev.of_node, "fsl,timeout", &plen);
  529. if (prop && plen == sizeof(u32)) {
  530. mpc_ops.timeout = *prop * HZ / 1000000;
  531. if (mpc_ops.timeout < 5)
  532. mpc_ops.timeout = 5;
  533. }
  534. dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
  535. dev_set_drvdata(&op->dev, i2c);
  536. i2c->adap = mpc_ops;
  537. i2c_set_adapdata(&i2c->adap, i2c);
  538. i2c->adap.dev.parent = &op->dev;
  539. i2c->adap.dev.of_node = of_node_get(op->dev.of_node);
  540. result = i2c_add_adapter(&i2c->adap);
  541. if (result < 0) {
  542. dev_err(i2c->dev, "failed to add adapter\n");
  543. goto fail_add;
  544. }
  545. of_i2c_register_devices(&i2c->adap);
  546. return result;
  547. fail_add:
  548. dev_set_drvdata(&op->dev, NULL);
  549. free_irq(i2c->irq, i2c);
  550. fail_request:
  551. irq_dispose_mapping(i2c->irq);
  552. iounmap(i2c->base);
  553. fail_map:
  554. kfree(i2c);
  555. return result;
  556. };
  557. static int __devexit fsl_i2c_remove(struct platform_device *op)
  558. {
  559. struct mpc_i2c *i2c = dev_get_drvdata(&op->dev);
  560. i2c_del_adapter(&i2c->adap);
  561. dev_set_drvdata(&op->dev, NULL);
  562. if (i2c->irq)
  563. free_irq(i2c->irq, i2c);
  564. irq_dispose_mapping(i2c->irq);
  565. iounmap(i2c->base);
  566. kfree(i2c);
  567. return 0;
  568. };
  569. static struct mpc_i2c_data mpc_i2c_data_512x __devinitdata = {
  570. .setup = mpc_i2c_setup_512x,
  571. };
  572. static struct mpc_i2c_data mpc_i2c_data_52xx __devinitdata = {
  573. .setup = mpc_i2c_setup_52xx,
  574. };
  575. static struct mpc_i2c_data mpc_i2c_data_8313 __devinitdata = {
  576. .setup = mpc_i2c_setup_8xxx,
  577. };
  578. static struct mpc_i2c_data mpc_i2c_data_8543 __devinitdata = {
  579. .setup = mpc_i2c_setup_8xxx,
  580. .prescaler = 2,
  581. };
  582. static struct mpc_i2c_data mpc_i2c_data_8544 __devinitdata = {
  583. .setup = mpc_i2c_setup_8xxx,
  584. .prescaler = 3,
  585. };
  586. static const struct of_device_id mpc_i2c_of_match[] = {
  587. {.compatible = "mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
  588. {.compatible = "fsl,mpc5200b-i2c", .data = &mpc_i2c_data_52xx, },
  589. {.compatible = "fsl,mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
  590. {.compatible = "fsl,mpc5121-i2c", .data = &mpc_i2c_data_512x, },
  591. {.compatible = "fsl,mpc8313-i2c", .data = &mpc_i2c_data_8313, },
  592. {.compatible = "fsl,mpc8543-i2c", .data = &mpc_i2c_data_8543, },
  593. {.compatible = "fsl,mpc8544-i2c", .data = &mpc_i2c_data_8544, },
  594. /* Backward compatibility */
  595. {.compatible = "fsl-i2c", },
  596. {},
  597. };
  598. MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);
  599. /* Structure for a device driver */
  600. static struct platform_driver mpc_i2c_driver = {
  601. .probe = fsl_i2c_probe,
  602. .remove = __devexit_p(fsl_i2c_remove),
  603. .driver = {
  604. .owner = THIS_MODULE,
  605. .name = DRV_NAME,
  606. .of_match_table = mpc_i2c_of_match,
  607. },
  608. };
  609. static int __init fsl_i2c_init(void)
  610. {
  611. return platform_driver_register(&mpc_i2c_driver);
  612. }
  613. static void __exit fsl_i2c_exit(void)
  614. {
  615. platform_driver_unregister(&mpc_i2c_driver);
  616. }
  617. module_init(fsl_i2c_init);
  618. module_exit(fsl_i2c_exit);
  619. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  620. MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "
  621. "MPC824x/83xx/85xx/86xx/512x/52xx processors");
  622. MODULE_LICENSE("GPL");