i2c-stu300.c 26 KB

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