/kern_2.6.32/drivers/i2c/busses/i2c-stu300.c

http://omnia2droid.googlecode.com/ · C · 1056 lines · 725 code · 140 blank · 191 comment · 113 complexity · 59d9063187e7479534deacbe6b6a01ed MD5 · raw file

  1. /*
  2. * Copyright (C) 2007-2009 ST-Ericsson AB
  3. * License terms: GNU General Public License (GPL) version 2
  4. * ST DDC I2C master mode driver, used in e.g. U300 series platforms.
  5. * Author: Linus Walleij <linus.walleij@stericsson.com>
  6. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/completion.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. /* the name of this kernel module */
  20. #define NAME "stu300"
  21. /* CR (Control Register) 8bit (R/W) */
  22. #define I2C_CR (0x00000000)
  23. #define I2C_CR_RESET_VALUE (0x00)
  24. #define I2C_CR_RESET_UMASK (0x00)
  25. #define I2C_CR_DDC1_ENABLE (0x80)
  26. #define I2C_CR_TRANS_ENABLE (0x40)
  27. #define I2C_CR_PERIPHERAL_ENABLE (0x20)
  28. #define I2C_CR_DDC2B_ENABLE (0x10)
  29. #define I2C_CR_START_ENABLE (0x08)
  30. #define I2C_CR_ACK_ENABLE (0x04)
  31. #define I2C_CR_STOP_ENABLE (0x02)
  32. #define I2C_CR_INTERRUPT_ENABLE (0x01)
  33. /* SR1 (Status Register 1) 8bit (R/-) */
  34. #define I2C_SR1 (0x00000004)
  35. #define I2C_SR1_RESET_VALUE (0x00)
  36. #define I2C_SR1_RESET_UMASK (0x00)
  37. #define I2C_SR1_EVF_IND (0x80)
  38. #define I2C_SR1_ADD10_IND (0x40)
  39. #define I2C_SR1_TRA_IND (0x20)
  40. #define I2C_SR1_BUSY_IND (0x10)
  41. #define I2C_SR1_BTF_IND (0x08)
  42. #define I2C_SR1_ADSL_IND (0x04)
  43. #define I2C_SR1_MSL_IND (0x02)
  44. #define I2C_SR1_SB_IND (0x01)
  45. /* SR2 (Status Register 2) 8bit (R/-) */
  46. #define I2C_SR2 (0x00000008)
  47. #define I2C_SR2_RESET_VALUE (0x00)
  48. #define I2C_SR2_RESET_UMASK (0x40)
  49. #define I2C_SR2_MASK (0xBF)
  50. #define I2C_SR2_SCLFAL_IND (0x80)
  51. #define I2C_SR2_ENDAD_IND (0x20)
  52. #define I2C_SR2_AF_IND (0x10)
  53. #define I2C_SR2_STOPF_IND (0x08)
  54. #define I2C_SR2_ARLO_IND (0x04)
  55. #define I2C_SR2_BERR_IND (0x02)
  56. #define I2C_SR2_DDC2BF_IND (0x01)
  57. /* CCR (Clock Control Register) 8bit (R/W) */
  58. #define I2C_CCR (0x0000000C)
  59. #define I2C_CCR_RESET_VALUE (0x00)
  60. #define I2C_CCR_RESET_UMASK (0x00)
  61. #define I2C_CCR_MASK (0xFF)
  62. #define I2C_CCR_FMSM (0x80)
  63. #define I2C_CCR_CC_MASK (0x7F)
  64. /* OAR1 (Own Address Register 1) 8bit (R/W) */
  65. #define I2C_OAR1 (0x00000010)
  66. #define I2C_OAR1_RESET_VALUE (0x00)
  67. #define I2C_OAR1_RESET_UMASK (0x00)
  68. #define I2C_OAR1_ADD_MASK (0xFF)
  69. /* OAR2 (Own Address Register 2) 8bit (R/W) */
  70. #define I2C_OAR2 (0x00000014)
  71. #define I2C_OAR2_RESET_VALUE (0x40)
  72. #define I2C_OAR2_RESET_UMASK (0x19)
  73. #define I2C_OAR2_MASK (0xE6)
  74. #define I2C_OAR2_FR_25_10MHZ (0x00)
  75. #define I2C_OAR2_FR_10_1667MHZ (0x20)
  76. #define I2C_OAR2_FR_1667_2667MHZ (0x40)
  77. #define I2C_OAR2_FR_2667_40MHZ (0x60)
  78. #define I2C_OAR2_FR_40_5333MHZ (0x80)
  79. #define I2C_OAR2_FR_5333_66MHZ (0xA0)
  80. #define I2C_OAR2_FR_66_80MHZ (0xC0)
  81. #define I2C_OAR2_FR_80_100MHZ (0xE0)
  82. #define I2C_OAR2_FR_MASK (0xE0)
  83. #define I2C_OAR2_ADD_MASK (0x06)
  84. /* DR (Data Register) 8bit (R/W) */
  85. #define I2C_DR (0x00000018)
  86. #define I2C_DR_RESET_VALUE (0x00)
  87. #define I2C_DR_RESET_UMASK (0xFF)
  88. #define I2C_DR_D_MASK (0xFF)
  89. /* ECCR (Extended Clock Control Register) 8bit (R/W) */
  90. #define I2C_ECCR (0x0000001C)
  91. #define I2C_ECCR_RESET_VALUE (0x00)
  92. #define I2C_ECCR_RESET_UMASK (0xE0)
  93. #define I2C_ECCR_MASK (0x1F)
  94. #define I2C_ECCR_CC_MASK (0x1F)
  95. /*
  96. * These events are more or less responses to commands
  97. * sent into the hardware, presumably reflecting the state
  98. * of an internal state machine.
  99. */
  100. enum stu300_event {
  101. STU300_EVENT_NONE = 0,
  102. STU300_EVENT_1,
  103. STU300_EVENT_2,
  104. STU300_EVENT_3,
  105. STU300_EVENT_4,
  106. STU300_EVENT_5,
  107. STU300_EVENT_6,
  108. STU300_EVENT_7,
  109. STU300_EVENT_8,
  110. STU300_EVENT_9
  111. };
  112. enum stu300_error {
  113. STU300_ERROR_NONE = 0,
  114. STU300_ERROR_ACKNOWLEDGE_FAILURE,
  115. STU300_ERROR_BUS_ERROR,
  116. STU300_ERROR_ARBITRATION_LOST,
  117. STU300_ERROR_UNKNOWN
  118. };
  119. /* timeout waiting for the controller to respond */
  120. #define STU300_TIMEOUT (msecs_to_jiffies(1000))
  121. /*
  122. * The number of address send athemps tried before giving up.
  123. * If the first one failes it seems like 5 to 8 attempts are required.
  124. */
  125. #define NUM_ADDR_RESEND_ATTEMPTS 12
  126. /* I2C clock speed, in Hz 0-400kHz*/
  127. static unsigned int scl_frequency = 100000;
  128. module_param(scl_frequency, uint, 0644);
  129. /**
  130. * struct stu300_dev - the stu300 driver state holder
  131. * @pdev: parent platform device
  132. * @adapter: corresponding I2C adapter
  133. * @phybase: location of I/O area in memory
  134. * @physize: size of I/O area in memory
  135. * @clk: hardware block clock
  136. * @irq: assigned interrupt line
  137. * @cmd_issue_lock: this locks the following cmd_ variables
  138. * @cmd_complete: acknowledge completion for an I2C command
  139. * @cmd_event: expected event coming in as a response to a command
  140. * @cmd_err: error code as response to a command
  141. * @speed: current bus speed in Hz
  142. * @msg_index: index of current message
  143. * @msg_len: length of current message
  144. */
  145. struct stu300_dev {
  146. struct platform_device *pdev;
  147. struct i2c_adapter adapter;
  148. resource_size_t phybase;
  149. resource_size_t physize;
  150. void __iomem *virtbase;
  151. struct clk *clk;
  152. int irq;
  153. spinlock_t cmd_issue_lock;
  154. struct completion cmd_complete;
  155. enum stu300_event cmd_event;
  156. enum stu300_error cmd_err;
  157. unsigned int speed;
  158. int msg_index;
  159. int msg_len;
  160. };
  161. /* Local forward function declarations */
  162. static int stu300_init_hw(struct stu300_dev *dev);
  163. /*
  164. * The block needs writes in both MSW and LSW in order
  165. * for all data lines to reach their destination.
  166. */
  167. static inline void stu300_wr8(u32 value, void __iomem *address)
  168. {
  169. writel((value << 16) | value, address);
  170. }
  171. /*
  172. * This merely masks off the duplicates which appear
  173. * in bytes 1-3. You _MUST_ use 32-bit bus access on this
  174. * device, else it will not work.
  175. */
  176. static inline u32 stu300_r8(void __iomem *address)
  177. {
  178. return readl(address) & 0x000000FFU;
  179. }
  180. static void stu300_irq_enable(struct stu300_dev *dev)
  181. {
  182. u32 val;
  183. val = stu300_r8(dev->virtbase + I2C_CR);
  184. val |= I2C_CR_INTERRUPT_ENABLE;
  185. /* Twice paranoia (possible HW glitch) */
  186. stu300_wr8(val, dev->virtbase + I2C_CR);
  187. stu300_wr8(val, dev->virtbase + I2C_CR);
  188. }
  189. static void stu300_irq_disable(struct stu300_dev *dev)
  190. {
  191. u32 val;
  192. val = stu300_r8(dev->virtbase + I2C_CR);
  193. val &= ~I2C_CR_INTERRUPT_ENABLE;
  194. /* Twice paranoia (possible HW glitch) */
  195. stu300_wr8(val, dev->virtbase + I2C_CR);
  196. stu300_wr8(val, dev->virtbase + I2C_CR);
  197. }
  198. /*
  199. * Tells whether a certain event or events occurred in
  200. * response to a command. The events represent states in
  201. * the internal state machine of the hardware. The events
  202. * are not very well described in the hardware
  203. * documentation and can only be treated as abstract state
  204. * machine states.
  205. *
  206. * @ret 0 = event has not occurred or unknown error, any
  207. * other value means the correct event occurred or an error.
  208. */
  209. static int stu300_event_occurred(struct stu300_dev *dev,
  210. enum stu300_event mr_event) {
  211. u32 status1;
  212. u32 status2;
  213. /* What event happened? */
  214. status1 = stu300_r8(dev->virtbase + I2C_SR1);
  215. if (!(status1 & I2C_SR1_EVF_IND))
  216. /* No event at all */
  217. return 0;
  218. status2 = stu300_r8(dev->virtbase + I2C_SR2);
  219. /* Block any multiple interrupts */
  220. stu300_irq_disable(dev);
  221. /* Check for errors first */
  222. if (status2 & I2C_SR2_AF_IND) {
  223. dev->cmd_err = STU300_ERROR_ACKNOWLEDGE_FAILURE;
  224. return 1;
  225. } else if (status2 & I2C_SR2_BERR_IND) {
  226. dev->cmd_err = STU300_ERROR_BUS_ERROR;
  227. return 1;
  228. } else if (status2 & I2C_SR2_ARLO_IND) {
  229. dev->cmd_err = STU300_ERROR_ARBITRATION_LOST;
  230. return 1;
  231. }
  232. switch (mr_event) {
  233. case STU300_EVENT_1:
  234. if (status1 & I2C_SR1_ADSL_IND)
  235. return 1;
  236. break;
  237. case STU300_EVENT_2:
  238. case STU300_EVENT_3:
  239. case STU300_EVENT_7:
  240. case STU300_EVENT_8:
  241. if (status1 & I2C_SR1_BTF_IND) {
  242. return 1;
  243. }
  244. break;
  245. case STU300_EVENT_4:
  246. if (status2 & I2C_SR2_STOPF_IND)
  247. return 1;
  248. break;
  249. case STU300_EVENT_5:
  250. if (status1 & I2C_SR1_SB_IND)
  251. /* Clear start bit */
  252. return 1;
  253. break;
  254. case STU300_EVENT_6:
  255. if (status2 & I2C_SR2_ENDAD_IND) {
  256. /* First check for any errors */
  257. return 1;
  258. }
  259. break;
  260. case STU300_EVENT_9:
  261. if (status1 & I2C_SR1_ADD10_IND)
  262. return 1;
  263. break;
  264. default:
  265. break;
  266. }
  267. /* If we get here, we're on thin ice.
  268. * Here we are in a status where we have
  269. * gotten a response that does not match
  270. * what we requested.
  271. */
  272. dev->cmd_err = STU300_ERROR_UNKNOWN;
  273. dev_err(&dev->pdev->dev,
  274. "Unhandled interrupt! %d sr1: 0x%x sr2: 0x%x\n",
  275. mr_event, status1, status2);
  276. return 0;
  277. }
  278. static irqreturn_t stu300_irh(int irq, void *data)
  279. {
  280. struct stu300_dev *dev = data;
  281. int res;
  282. /* Just make sure that the block is clocked */
  283. clk_enable(dev->clk);
  284. /* See if this was what we were waiting for */
  285. spin_lock(&dev->cmd_issue_lock);
  286. res = stu300_event_occurred(dev, dev->cmd_event);
  287. if (res || dev->cmd_err != STU300_ERROR_NONE)
  288. complete(&dev->cmd_complete);
  289. spin_unlock(&dev->cmd_issue_lock);
  290. clk_disable(dev->clk);
  291. return IRQ_HANDLED;
  292. }
  293. /*
  294. * Sends a command and then waits for the bits masked by *flagmask*
  295. * to go high or low by IRQ awaiting.
  296. */
  297. static int stu300_start_and_await_event(struct stu300_dev *dev,
  298. u8 cr_value,
  299. enum stu300_event mr_event)
  300. {
  301. int ret;
  302. if (unlikely(irqs_disabled())) {
  303. /* TODO: implement polling for this case if need be. */
  304. WARN(1, "irqs are disabled, cannot poll for event\n");
  305. return -EIO;
  306. }
  307. /* Lock command issue, fill in an event we wait for */
  308. spin_lock_irq(&dev->cmd_issue_lock);
  309. init_completion(&dev->cmd_complete);
  310. dev->cmd_err = STU300_ERROR_NONE;
  311. dev->cmd_event = mr_event;
  312. spin_unlock_irq(&dev->cmd_issue_lock);
  313. /* Turn on interrupt, send command and wait. */
  314. cr_value |= I2C_CR_INTERRUPT_ENABLE;
  315. stu300_wr8(cr_value, dev->virtbase + I2C_CR);
  316. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  317. STU300_TIMEOUT);
  318. if (ret < 0) {
  319. dev_err(&dev->pdev->dev,
  320. "wait_for_completion_interruptible_timeout() "
  321. "returned %d waiting for event %04x\n", ret, mr_event);
  322. return ret;
  323. }
  324. if (ret == 0) {
  325. dev_err(&dev->pdev->dev, "controller timed out "
  326. "waiting for event %d, reinit hardware\n", mr_event);
  327. (void) stu300_init_hw(dev);
  328. return -ETIMEDOUT;
  329. }
  330. if (dev->cmd_err != STU300_ERROR_NONE) {
  331. dev_err(&dev->pdev->dev, "controller (start) "
  332. "error %d waiting for event %d, reinit hardware\n",
  333. dev->cmd_err, mr_event);
  334. (void) stu300_init_hw(dev);
  335. return -EIO;
  336. }
  337. return 0;
  338. }
  339. /*
  340. * This waits for a flag to be set, if it is not set on entry, an interrupt is
  341. * configured to wait for the flag using a completion.
  342. */
  343. static int stu300_await_event(struct stu300_dev *dev,
  344. enum stu300_event mr_event)
  345. {
  346. int ret;
  347. if (unlikely(irqs_disabled())) {
  348. /* TODO: implement polling for this case if need be. */
  349. dev_err(&dev->pdev->dev, "irqs are disabled on this "
  350. "system!\n");
  351. return -EIO;
  352. }
  353. /* Is it already here? */
  354. spin_lock_irq(&dev->cmd_issue_lock);
  355. dev->cmd_err = STU300_ERROR_NONE;
  356. dev->cmd_event = mr_event;
  357. init_completion(&dev->cmd_complete);
  358. /* Turn on the I2C interrupt for current operation */
  359. stu300_irq_enable(dev);
  360. /* Unlock the command block and wait for the event to occur */
  361. spin_unlock_irq(&dev->cmd_issue_lock);
  362. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  363. STU300_TIMEOUT);
  364. if (ret < 0) {
  365. dev_err(&dev->pdev->dev,
  366. "wait_for_completion_interruptible_timeout()"
  367. "returned %d waiting for event %04x\n", ret, mr_event);
  368. return ret;
  369. }
  370. if (ret == 0) {
  371. if (mr_event != STU300_EVENT_6) {
  372. dev_err(&dev->pdev->dev, "controller "
  373. "timed out waiting for event %d, reinit "
  374. "hardware\n", mr_event);
  375. (void) stu300_init_hw(dev);
  376. }
  377. return -ETIMEDOUT;
  378. }
  379. if (dev->cmd_err != STU300_ERROR_NONE) {
  380. if (mr_event != STU300_EVENT_6) {
  381. dev_err(&dev->pdev->dev, "controller "
  382. "error (await_event) %d waiting for event %d, "
  383. "reinit hardware\n", dev->cmd_err, mr_event);
  384. (void) stu300_init_hw(dev);
  385. }
  386. return -EIO;
  387. }
  388. return 0;
  389. }
  390. /*
  391. * Waits for the busy bit to go low by repeated polling.
  392. */
  393. #define BUSY_RELEASE_ATTEMPTS 10
  394. static int stu300_wait_while_busy(struct stu300_dev *dev)
  395. {
  396. unsigned long timeout;
  397. int i;
  398. for (i = 0; i < BUSY_RELEASE_ATTEMPTS; i++) {
  399. timeout = jiffies + STU300_TIMEOUT;
  400. while (!time_after(jiffies, timeout)) {
  401. /* Is not busy? */
  402. if ((stu300_r8(dev->virtbase + I2C_SR1) &
  403. I2C_SR1_BUSY_IND) == 0)
  404. return 0;
  405. msleep(1);
  406. }
  407. dev_err(&dev->pdev->dev, "transaction timed out "
  408. "waiting for device to be free (not busy). "
  409. "Attempt: %d\n", i+1);
  410. dev_err(&dev->pdev->dev, "base address = "
  411. "0x%08x, reinit hardware\n", (u32) dev->virtbase);
  412. (void) stu300_init_hw(dev);
  413. }
  414. dev_err(&dev->pdev->dev, "giving up after %d attempts "
  415. "to reset the bus.\n", BUSY_RELEASE_ATTEMPTS);
  416. return -ETIMEDOUT;
  417. }
  418. struct stu300_clkset {
  419. unsigned long rate;
  420. u32 setting;
  421. };
  422. static const struct stu300_clkset stu300_clktable[] = {
  423. { 0, 0xFFU },
  424. { 2500000, I2C_OAR2_FR_25_10MHZ },
  425. { 10000000, I2C_OAR2_FR_10_1667MHZ },
  426. { 16670000, I2C_OAR2_FR_1667_2667MHZ },
  427. { 26670000, I2C_OAR2_FR_2667_40MHZ },
  428. { 40000000, I2C_OAR2_FR_40_5333MHZ },
  429. { 53330000, I2C_OAR2_FR_5333_66MHZ },
  430. { 66000000, I2C_OAR2_FR_66_80MHZ },
  431. { 80000000, I2C_OAR2_FR_80_100MHZ },
  432. { 100000000, 0xFFU },
  433. };
  434. static int stu300_set_clk(struct stu300_dev *dev, unsigned long clkrate)
  435. {
  436. u32 val;
  437. int i = 0;
  438. /* Locate the apropriate clock setting */
  439. while (i < ARRAY_SIZE(stu300_clktable) &&
  440. stu300_clktable[i].rate < clkrate)
  441. i++;
  442. if (stu300_clktable[i].setting == 0xFFU) {
  443. dev_err(&dev->pdev->dev, "too %s clock rate requested "
  444. "(%lu Hz).\n", i ? "high" : "low", clkrate);
  445. return -EINVAL;
  446. }
  447. stu300_wr8(stu300_clktable[i].setting,
  448. dev->virtbase + I2C_OAR2);
  449. dev_dbg(&dev->pdev->dev, "Clock rate %lu Hz, I2C bus speed %d Hz "
  450. "virtbase %p\n", clkrate, dev->speed, dev->virtbase);
  451. if (dev->speed > 100000)
  452. /* Fast Mode I2C */
  453. val = ((clkrate/dev->speed) - 9)/3 + 1;
  454. else
  455. /* Standard Mode I2C */
  456. val = ((clkrate/dev->speed) - 7)/2 + 1;
  457. /* According to spec the divider must be > 2 */
  458. if (val < 0x002) {
  459. dev_err(&dev->pdev->dev, "too low clock rate (%lu Hz).\n",
  460. clkrate);
  461. return -EINVAL;
  462. }
  463. /* We have 12 bits clock divider only! */
  464. if (val & 0xFFFFF000U) {
  465. dev_err(&dev->pdev->dev, "too high clock rate (%lu Hz).\n",
  466. clkrate);
  467. return -EINVAL;
  468. }
  469. if (dev->speed > 100000) {
  470. /* CC6..CC0 */
  471. stu300_wr8((val & I2C_CCR_CC_MASK) | I2C_CCR_FMSM,
  472. dev->virtbase + I2C_CCR);
  473. dev_dbg(&dev->pdev->dev, "set clock divider to 0x%08x, "
  474. "Fast Mode I2C\n", val);
  475. } else {
  476. /* CC6..CC0 */
  477. stu300_wr8((val & I2C_CCR_CC_MASK),
  478. dev->virtbase + I2C_CCR);
  479. dev_dbg(&dev->pdev->dev, "set clock divider to "
  480. "0x%08x, Standard Mode I2C\n", val);
  481. }
  482. /* CC11..CC7 */
  483. stu300_wr8(((val >> 7) & 0x1F),
  484. dev->virtbase + I2C_ECCR);
  485. return 0;
  486. }
  487. static int stu300_init_hw(struct stu300_dev *dev)
  488. {
  489. u32 dummy;
  490. unsigned long clkrate;
  491. int ret;
  492. /* Disable controller */
  493. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  494. /*
  495. * Set own address to some default value (0x00).
  496. * We do not support slave mode anyway.
  497. */
  498. stu300_wr8(0x00, dev->virtbase + I2C_OAR1);
  499. /*
  500. * The I2C controller only operates properly in 26 MHz but we
  501. * program this driver as if we didn't know. This will also set the two
  502. * high bits of the own address to zero as well.
  503. * There is no known hardware issue with running in 13 MHz
  504. * However, speeds over 200 kHz are not used.
  505. */
  506. clkrate = clk_get_rate(dev->clk);
  507. ret = stu300_set_clk(dev, clkrate);
  508. if (ret)
  509. return ret;
  510. /*
  511. * Enable block, do it TWICE (hardware glitch)
  512. * Setting bit 7 can enable DDC mode. (Not used currently.)
  513. */
  514. stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
  515. dev->virtbase + I2C_CR);
  516. stu300_wr8(I2C_CR_PERIPHERAL_ENABLE,
  517. dev->virtbase + I2C_CR);
  518. /* Make a dummy read of the status register SR1 & SR2 */
  519. dummy = stu300_r8(dev->virtbase + I2C_SR2);
  520. dummy = stu300_r8(dev->virtbase + I2C_SR1);
  521. return 0;
  522. }
  523. /* Send slave address. */
  524. static int stu300_send_address(struct stu300_dev *dev,
  525. struct i2c_msg *msg, int resend)
  526. {
  527. u32 val;
  528. int ret;
  529. if (msg->flags & I2C_M_TEN)
  530. /* This is probably how 10 bit addresses look */
  531. val = (0xf0 | (((u32) msg->addr & 0x300) >> 7)) &
  532. I2C_DR_D_MASK;
  533. else
  534. val = ((msg->addr << 1) & I2C_DR_D_MASK);
  535. if (msg->flags & I2C_M_RD) {
  536. /* This is the direction bit */
  537. val |= 0x01;
  538. if (resend)
  539. dev_dbg(&dev->pdev->dev, "read resend\n");
  540. } else if (resend)
  541. dev_dbg(&dev->pdev->dev, "write resend\n");
  542. stu300_wr8(val, dev->virtbase + I2C_DR);
  543. /* For 10bit addressing, await 10bit request (EVENT 9) */
  544. if (msg->flags & I2C_M_TEN) {
  545. ret = stu300_await_event(dev, STU300_EVENT_9);
  546. /*
  547. * The slave device wants a 10bit address, send the rest
  548. * of the bits (the LSBits)
  549. */
  550. val = msg->addr & I2C_DR_D_MASK;
  551. /* This clears "event 9" */
  552. stu300_wr8(val, dev->virtbase + I2C_DR);
  553. if (ret != 0)
  554. return ret;
  555. }
  556. /* FIXME: Why no else here? two events for 10bit?
  557. * Await event 6 (normal) or event 9 (10bit)
  558. */
  559. if (resend)
  560. dev_dbg(&dev->pdev->dev, "await event 6\n");
  561. ret = stu300_await_event(dev, STU300_EVENT_6);
  562. /*
  563. * Clear any pending EVENT 6 no matter what happend during
  564. * await_event.
  565. */
  566. val = stu300_r8(dev->virtbase + I2C_CR);
  567. val |= I2C_CR_PERIPHERAL_ENABLE;
  568. stu300_wr8(val, dev->virtbase + I2C_CR);
  569. return ret;
  570. }
  571. static int stu300_xfer_msg(struct i2c_adapter *adap,
  572. struct i2c_msg *msg, int stop)
  573. {
  574. u32 cr;
  575. u32 val;
  576. u32 i;
  577. int ret;
  578. int attempts = 0;
  579. struct stu300_dev *dev = i2c_get_adapdata(adap);
  580. clk_enable(dev->clk);
  581. /* Remove this if (0) to trace each and every message. */
  582. if (0) {
  583. dev_dbg(&dev->pdev->dev, "I2C message to: 0x%04x, len: %d, "
  584. "flags: 0x%04x, stop: %d\n",
  585. msg->addr, msg->len, msg->flags, stop);
  586. }
  587. /* Zero-length messages are not supported by this hardware */
  588. if (msg->len == 0) {
  589. ret = -EINVAL;
  590. goto exit_disable;
  591. }
  592. /*
  593. * For some reason, sending the address sometimes fails when running
  594. * on the 13 MHz clock. No interrupt arrives. This is a work around,
  595. * which tries to restart and send the address up to 10 times before
  596. * really giving up. Usually 5 to 8 attempts are enough.
  597. */
  598. do {
  599. if (attempts)
  600. dev_dbg(&dev->pdev->dev, "wait while busy\n");
  601. /* Check that the bus is free, or wait until some timeout */
  602. ret = stu300_wait_while_busy(dev);
  603. if (ret != 0)
  604. goto exit_disable;
  605. if (attempts)
  606. dev_dbg(&dev->pdev->dev, "re-int hw\n");
  607. /*
  608. * According to ST, there is no problem if the clock is
  609. * changed between 13 and 26 MHz during a transfer.
  610. */
  611. ret = stu300_init_hw(dev);
  612. if (ret)
  613. goto exit_disable;
  614. /* Send a start condition */
  615. cr = I2C_CR_PERIPHERAL_ENABLE;
  616. /* Setting the START bit puts the block in master mode */
  617. if (!(msg->flags & I2C_M_NOSTART))
  618. cr |= I2C_CR_START_ENABLE;
  619. if ((msg->flags & I2C_M_RD) && (msg->len > 1))
  620. /* On read more than 1 byte, we need ack. */
  621. cr |= I2C_CR_ACK_ENABLE;
  622. /* Check that it gets through */
  623. if (!(msg->flags & I2C_M_NOSTART)) {
  624. if (attempts)
  625. dev_dbg(&dev->pdev->dev, "send start event\n");
  626. ret = stu300_start_and_await_event(dev, cr,
  627. STU300_EVENT_5);
  628. }
  629. if (attempts)
  630. dev_dbg(&dev->pdev->dev, "send address\n");
  631. if (ret == 0)
  632. /* Send address */
  633. ret = stu300_send_address(dev, msg, attempts != 0);
  634. if (ret != 0) {
  635. attempts++;
  636. dev_dbg(&dev->pdev->dev, "failed sending address, "
  637. "retrying. Attempt: %d msg_index: %d/%d\n",
  638. attempts, dev->msg_index, dev->msg_len);
  639. }
  640. } while (ret != 0 && attempts < NUM_ADDR_RESEND_ATTEMPTS);
  641. if (attempts < NUM_ADDR_RESEND_ATTEMPTS && attempts > 0) {
  642. dev_dbg(&dev->pdev->dev, "managed to get address "
  643. "through after %d attempts\n", attempts);
  644. } else if (attempts == NUM_ADDR_RESEND_ATTEMPTS) {
  645. dev_dbg(&dev->pdev->dev, "I give up, tried %d times "
  646. "to resend address.\n",
  647. NUM_ADDR_RESEND_ATTEMPTS);
  648. goto exit_disable;
  649. }
  650. if (msg->flags & I2C_M_RD) {
  651. /* READ: we read the actual bytes one at a time */
  652. for (i = 0; i < msg->len; i++) {
  653. if (i == msg->len-1) {
  654. /*
  655. * Disable ACK and set STOP condition before
  656. * reading last byte
  657. */
  658. val = I2C_CR_PERIPHERAL_ENABLE;
  659. if (stop)
  660. val |= I2C_CR_STOP_ENABLE;
  661. stu300_wr8(val,
  662. dev->virtbase + I2C_CR);
  663. }
  664. /* Wait for this byte... */
  665. ret = stu300_await_event(dev, STU300_EVENT_7);
  666. if (ret != 0)
  667. goto exit_disable;
  668. /* This clears event 7 */
  669. msg->buf[i] = (u8) stu300_r8(dev->virtbase + I2C_DR);
  670. }
  671. } else {
  672. /* WRITE: we send the actual bytes one at a time */
  673. for (i = 0; i < msg->len; i++) {
  674. /* Write the byte */
  675. stu300_wr8(msg->buf[i],
  676. dev->virtbase + I2C_DR);
  677. /* Check status */
  678. ret = stu300_await_event(dev, STU300_EVENT_8);
  679. /* Next write to DR will clear event 8 */
  680. if (ret != 0) {
  681. dev_err(&dev->pdev->dev, "error awaiting "
  682. "event 8 (%d)\n", ret);
  683. goto exit_disable;
  684. }
  685. }
  686. /* Check NAK */
  687. if (!(msg->flags & I2C_M_IGNORE_NAK)) {
  688. if (stu300_r8(dev->virtbase + I2C_SR2) &
  689. I2C_SR2_AF_IND) {
  690. dev_err(&dev->pdev->dev, "I2C payload "
  691. "send returned NAK!\n");
  692. ret = -EIO;
  693. goto exit_disable;
  694. }
  695. }
  696. if (stop) {
  697. /* Send stop condition */
  698. val = I2C_CR_PERIPHERAL_ENABLE;
  699. val |= I2C_CR_STOP_ENABLE;
  700. stu300_wr8(val, dev->virtbase + I2C_CR);
  701. }
  702. }
  703. /* Check that the bus is free, or wait until some timeout occurs */
  704. ret = stu300_wait_while_busy(dev);
  705. if (ret != 0) {
  706. dev_err(&dev->pdev->dev, "timout waiting for transfer "
  707. "to commence.\n");
  708. goto exit_disable;
  709. }
  710. /* Dummy read status registers */
  711. val = stu300_r8(dev->virtbase + I2C_SR2);
  712. val = stu300_r8(dev->virtbase + I2C_SR1);
  713. ret = 0;
  714. exit_disable:
  715. /* Disable controller */
  716. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  717. clk_disable(dev->clk);
  718. return ret;
  719. }
  720. static int stu300_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  721. int num)
  722. {
  723. int ret = -1;
  724. int i;
  725. struct stu300_dev *dev = i2c_get_adapdata(adap);
  726. dev->msg_len = num;
  727. for (i = 0; i < num; i++) {
  728. /*
  729. * Another driver appears to send stop for each message,
  730. * here we only do that for the last message. Possibly some
  731. * peripherals require this behaviour, then their drivers
  732. * have to send single messages in order to get "stop" for
  733. * each message.
  734. */
  735. dev->msg_index = i;
  736. ret = stu300_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  737. if (ret != 0) {
  738. num = ret;
  739. break;
  740. }
  741. }
  742. return num;
  743. }
  744. static u32 stu300_func(struct i2c_adapter *adap)
  745. {
  746. /* This is the simplest thing you can think of... */
  747. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  748. }
  749. static const struct i2c_algorithm stu300_algo = {
  750. .master_xfer = stu300_xfer,
  751. .functionality = stu300_func,
  752. };
  753. static int __init
  754. stu300_probe(struct platform_device *pdev)
  755. {
  756. struct stu300_dev *dev;
  757. struct i2c_adapter *adap;
  758. struct resource *res;
  759. int bus_nr;
  760. int ret = 0;
  761. char clk_name[] = "I2C0";
  762. dev = kzalloc(sizeof(struct stu300_dev), GFP_KERNEL);
  763. if (!dev) {
  764. dev_err(&pdev->dev, "could not allocate device struct\n");
  765. ret = -ENOMEM;
  766. goto err_no_devmem;
  767. }
  768. bus_nr = pdev->id;
  769. clk_name[3] += (char)bus_nr;
  770. dev->clk = clk_get(&pdev->dev, clk_name);
  771. if (IS_ERR(dev->clk)) {
  772. ret = PTR_ERR(dev->clk);
  773. dev_err(&pdev->dev, "could not retrieve i2c bus clock\n");
  774. goto err_no_clk;
  775. }
  776. dev->pdev = pdev;
  777. platform_set_drvdata(pdev, dev);
  778. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  779. if (!res) {
  780. ret = -ENOENT;
  781. goto err_no_resource;
  782. }
  783. dev->phybase = res->start;
  784. dev->physize = resource_size(res);
  785. if (request_mem_region(dev->phybase, dev->physize,
  786. NAME " I/O Area") == NULL) {
  787. ret = -EBUSY;
  788. goto err_no_ioregion;
  789. }
  790. dev->virtbase = ioremap(dev->phybase, dev->physize);
  791. dev_dbg(&pdev->dev, "initialize bus device I2C%d on virtual "
  792. "base %p\n", bus_nr, dev->virtbase);
  793. if (!dev->virtbase) {
  794. ret = -ENOMEM;
  795. goto err_no_ioremap;
  796. }
  797. dev->irq = platform_get_irq(pdev, 0);
  798. if (request_irq(dev->irq, stu300_irh, IRQF_DISABLED,
  799. NAME, dev)) {
  800. ret = -EIO;
  801. goto err_no_irq;
  802. }
  803. dev->speed = scl_frequency;
  804. clk_enable(dev->clk);
  805. ret = stu300_init_hw(dev);
  806. clk_disable(dev->clk);
  807. if (ret != 0) {
  808. dev_err(&dev->pdev->dev, "error initializing hardware.\n");
  809. goto err_init_hw;
  810. }
  811. /* IRQ event handling initialization */
  812. spin_lock_init(&dev->cmd_issue_lock);
  813. dev->cmd_event = STU300_EVENT_NONE;
  814. dev->cmd_err = STU300_ERROR_NONE;
  815. adap = &dev->adapter;
  816. adap->owner = THIS_MODULE;
  817. /* DDC class but actually often used for more generic I2C */
  818. adap->class = I2C_CLASS_DDC;
  819. strncpy(adap->name, "ST Microelectronics DDC I2C adapter",
  820. sizeof(adap->name));
  821. adap->nr = bus_nr;
  822. adap->algo = &stu300_algo;
  823. adap->dev.parent = &pdev->dev;
  824. i2c_set_adapdata(adap, dev);
  825. /* i2c device drivers may be active on return from add_adapter() */
  826. ret = i2c_add_numbered_adapter(adap);
  827. if (ret) {
  828. dev_err(&dev->pdev->dev, "failure adding ST Micro DDC "
  829. "I2C adapter\n");
  830. goto err_add_adapter;
  831. }
  832. return 0;
  833. err_add_adapter:
  834. err_init_hw:
  835. free_irq(dev->irq, dev);
  836. err_no_irq:
  837. iounmap(dev->virtbase);
  838. err_no_ioremap:
  839. release_mem_region(dev->phybase, dev->physize);
  840. err_no_ioregion:
  841. platform_set_drvdata(pdev, NULL);
  842. err_no_resource:
  843. clk_put(dev->clk);
  844. err_no_clk:
  845. kfree(dev);
  846. err_no_devmem:
  847. dev_err(&pdev->dev, "failed to add " NAME " adapter: %d\n",
  848. pdev->id);
  849. return ret;
  850. }
  851. #ifdef CONFIG_PM
  852. static int stu300_suspend(struct platform_device *pdev, pm_message_t state)
  853. {
  854. struct stu300_dev *dev = platform_get_drvdata(pdev);
  855. /* Turn off everything */
  856. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  857. return 0;
  858. }
  859. static int stu300_resume(struct platform_device *pdev)
  860. {
  861. int ret = 0;
  862. struct stu300_dev *dev = platform_get_drvdata(pdev);
  863. clk_enable(dev->clk);
  864. ret = stu300_init_hw(dev);
  865. clk_disable(dev->clk);
  866. if (ret != 0)
  867. dev_err(&pdev->dev, "error re-initializing hardware.\n");
  868. return ret;
  869. }
  870. #else
  871. #define stu300_suspend NULL
  872. #define stu300_resume NULL
  873. #endif
  874. static int __exit
  875. stu300_remove(struct platform_device *pdev)
  876. {
  877. struct stu300_dev *dev = platform_get_drvdata(pdev);
  878. i2c_del_adapter(&dev->adapter);
  879. /* Turn off everything */
  880. stu300_wr8(0x00, dev->virtbase + I2C_CR);
  881. free_irq(dev->irq, dev);
  882. iounmap(dev->virtbase);
  883. release_mem_region(dev->phybase, dev->physize);
  884. clk_put(dev->clk);
  885. platform_set_drvdata(pdev, NULL);
  886. kfree(dev);
  887. return 0;
  888. }
  889. static struct platform_driver stu300_i2c_driver = {
  890. .driver = {
  891. .name = NAME,
  892. .owner = THIS_MODULE,
  893. },
  894. .remove = __exit_p(stu300_remove),
  895. .suspend = stu300_suspend,
  896. .resume = stu300_resume,
  897. };
  898. static int __init stu300_init(void)
  899. {
  900. return platform_driver_probe(&stu300_i2c_driver, stu300_probe);
  901. }
  902. static void __exit stu300_exit(void)
  903. {
  904. platform_driver_unregister(&stu300_i2c_driver);
  905. }
  906. /*
  907. * The systems using this bus often have very basic devices such
  908. * as regulators on the I2C bus, so this needs to be loaded early.
  909. * Therefore it is registered in the subsys_initcall().
  910. */
  911. subsys_initcall(stu300_init);
  912. module_exit(stu300_exit);
  913. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  914. MODULE_DESCRIPTION("ST Micro DDC I2C adapter (" NAME ")");
  915. MODULE_LICENSE("GPL");
  916. MODULE_ALIAS("platform:" NAME);