i2c-stu300.c 27 KB

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