blob: ae0d15c3180a20d7c6cfb64db5dda01eb35d3082 [file] [log] [blame]
Benjamin Gaignard9c41e452017-11-30 09:43:57 +01001// SPDX-License-Identifier: GPL-2.0
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +02002/*
3 * Driver for STMicroelectronics STM32F7 I2C controller
4 *
5 * This I2C controller is described in the STM32F75xxx and STM32F74xxx Soc
6 * reference manual.
7 * Please see below a link to the documentation:
8 * http://www.st.com/resource/en/reference_manual/dm00124865.pdf
9 *
10 * Copyright (C) M'boumba Cedric Madianga 2017
Benjamin Gaignard9c41e452017-11-30 09:43:57 +010011 * Copyright (C) STMicroelectronics 2017
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +020012 * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
13 *
14 * This driver is based on i2c-stm32f4.c
15 *
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +020016 */
17#include <linux/clk.h>
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/i2c.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/iopoll.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_irq.h>
28#include <linux/of_platform.h>
29#include <linux/platform_device.h>
30#include <linux/reset.h>
31#include <linux/slab.h>
32
33#include "i2c-stm32.h"
34
35/* STM32F7 I2C registers */
36#define STM32F7_I2C_CR1 0x00
37#define STM32F7_I2C_CR2 0x04
38#define STM32F7_I2C_TIMINGR 0x10
39#define STM32F7_I2C_ISR 0x18
40#define STM32F7_I2C_ICR 0x1C
41#define STM32F7_I2C_RXDR 0x24
42#define STM32F7_I2C_TXDR 0x28
43
44/* STM32F7 I2C control 1 */
45#define STM32F7_I2C_CR1_ANFOFF BIT(12)
46#define STM32F7_I2C_CR1_ERRIE BIT(7)
47#define STM32F7_I2C_CR1_TCIE BIT(6)
48#define STM32F7_I2C_CR1_STOPIE BIT(5)
49#define STM32F7_I2C_CR1_NACKIE BIT(4)
50#define STM32F7_I2C_CR1_ADDRIE BIT(3)
51#define STM32F7_I2C_CR1_RXIE BIT(2)
52#define STM32F7_I2C_CR1_TXIE BIT(1)
53#define STM32F7_I2C_CR1_PE BIT(0)
54#define STM32F7_I2C_ALL_IRQ_MASK (STM32F7_I2C_CR1_ERRIE \
55 | STM32F7_I2C_CR1_TCIE \
56 | STM32F7_I2C_CR1_STOPIE \
57 | STM32F7_I2C_CR1_NACKIE \
58 | STM32F7_I2C_CR1_RXIE \
59 | STM32F7_I2C_CR1_TXIE)
60
61/* STM32F7 I2C control 2 */
62#define STM32F7_I2C_CR2_RELOAD BIT(24)
63#define STM32F7_I2C_CR2_NBYTES_MASK GENMASK(23, 16)
64#define STM32F7_I2C_CR2_NBYTES(n) (((n) & 0xff) << 16)
65#define STM32F7_I2C_CR2_NACK BIT(15)
66#define STM32F7_I2C_CR2_STOP BIT(14)
67#define STM32F7_I2C_CR2_START BIT(13)
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +020068#define STM32F7_I2C_CR2_HEAD10R BIT(12)
69#define STM32F7_I2C_CR2_ADD10 BIT(11)
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +020070#define STM32F7_I2C_CR2_RD_WRN BIT(10)
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +020071#define STM32F7_I2C_CR2_SADD10_MASK GENMASK(9, 0)
72#define STM32F7_I2C_CR2_SADD10(n) (((n) & \
73 STM32F7_I2C_CR2_SADD10_MASK))
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +020074#define STM32F7_I2C_CR2_SADD7_MASK GENMASK(7, 1)
75#define STM32F7_I2C_CR2_SADD7(n) (((n) & 0x7f) << 1)
76
77/* STM32F7 I2C Interrupt Status */
78#define STM32F7_I2C_ISR_BUSY BIT(15)
79#define STM32F7_I2C_ISR_ARLO BIT(9)
80#define STM32F7_I2C_ISR_BERR BIT(8)
81#define STM32F7_I2C_ISR_TCR BIT(7)
82#define STM32F7_I2C_ISR_TC BIT(6)
83#define STM32F7_I2C_ISR_STOPF BIT(5)
84#define STM32F7_I2C_ISR_NACKF BIT(4)
85#define STM32F7_I2C_ISR_RXNE BIT(2)
86#define STM32F7_I2C_ISR_TXIS BIT(1)
87
88/* STM32F7 I2C Interrupt Clear */
89#define STM32F7_I2C_ICR_ARLOCF BIT(9)
90#define STM32F7_I2C_ICR_BERRCF BIT(8)
91#define STM32F7_I2C_ICR_STOPCF BIT(5)
92#define STM32F7_I2C_ICR_NACKCF BIT(4)
93
94/* STM32F7 I2C Timing */
95#define STM32F7_I2C_TIMINGR_PRESC(n) (((n) & 0xf) << 28)
96#define STM32F7_I2C_TIMINGR_SCLDEL(n) (((n) & 0xf) << 20)
97#define STM32F7_I2C_TIMINGR_SDADEL(n) (((n) & 0xf) << 16)
98#define STM32F7_I2C_TIMINGR_SCLH(n) (((n) & 0xff) << 8)
99#define STM32F7_I2C_TIMINGR_SCLL(n) ((n) & 0xff)
100
101#define STM32F7_I2C_MAX_LEN 0xff
102
103#define STM32F7_I2C_DNF_DEFAULT 0
104#define STM32F7_I2C_DNF_MAX 16
105
106#define STM32F7_I2C_ANALOG_FILTER_ENABLE 1
107#define STM32F7_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */
108#define STM32F7_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */
109
110#define STM32F7_I2C_RISE_TIME_DEFAULT 25 /* ns */
111#define STM32F7_I2C_FALL_TIME_DEFAULT 10 /* ns */
112
113#define STM32F7_PRESC_MAX BIT(4)
114#define STM32F7_SCLDEL_MAX BIT(4)
115#define STM32F7_SDADEL_MAX BIT(4)
116#define STM32F7_SCLH_MAX BIT(8)
117#define STM32F7_SCLL_MAX BIT(8)
118
119/**
120 * struct stm32f7_i2c_spec - private i2c specification timing
121 * @rate: I2C bus speed (Hz)
122 * @rate_min: 80% of I2C bus speed (Hz)
123 * @rate_max: 100% of I2C bus speed (Hz)
124 * @fall_max: Max fall time of both SDA and SCL signals (ns)
125 * @rise_max: Max rise time of both SDA and SCL signals (ns)
126 * @hddat_min: Min data hold time (ns)
127 * @vddat_max: Max data valid time (ns)
128 * @sudat_min: Min data setup time (ns)
129 * @l_min: Min low period of the SCL clock (ns)
130 * @h_min: Min high period of the SCL clock (ns)
131 */
132struct stm32f7_i2c_spec {
133 u32 rate;
134 u32 rate_min;
135 u32 rate_max;
136 u32 fall_max;
137 u32 rise_max;
138 u32 hddat_min;
139 u32 vddat_max;
140 u32 sudat_min;
141 u32 l_min;
142 u32 h_min;
143};
144
145/**
146 * struct stm32f7_i2c_setup - private I2C timing setup parameters
147 * @speed: I2C speed mode (standard, Fast Plus)
148 * @speed_freq: I2C speed frequency (Hz)
149 * @clock_src: I2C clock source frequency (Hz)
150 * @rise_time: Rise time (ns)
151 * @fall_time: Fall time (ns)
152 * @dnf: Digital filter coefficient (0-16)
153 * @analog_filter: Analog filter delay (On/Off)
154 */
155struct stm32f7_i2c_setup {
156 enum stm32_i2c_speed speed;
157 u32 speed_freq;
158 u32 clock_src;
159 u32 rise_time;
160 u32 fall_time;
161 u8 dnf;
162 bool analog_filter;
163};
164
165/**
166 * struct stm32f7_i2c_timings - private I2C output parameters
167 * @prec: Prescaler value
168 * @scldel: Data setup time
169 * @sdadel: Data hold time
170 * @sclh: SCL high period (master mode)
171 * @sclh: SCL low period (master mode)
172 */
173struct stm32f7_i2c_timings {
174 struct list_head node;
175 u8 presc;
176 u8 scldel;
177 u8 sdadel;
178 u8 sclh;
179 u8 scll;
180};
181
182/**
183 * struct stm32f7_i2c_msg - client specific data
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +0200184 * @addr: 8-bit or 10-bit slave addr, including r/w bit
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200185 * @count: number of bytes to be transferred
186 * @buf: data buffer
187 * @result: result of the transfer
188 * @stop: last I2C msg to be sent, i.e. STOP to be generated
189 */
190struct stm32f7_i2c_msg {
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +0200191 u16 addr;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200192 u32 count;
193 u8 *buf;
194 int result;
195 bool stop;
196};
197
198/**
199 * struct stm32f7_i2c_dev - private data of the controller
200 * @adap: I2C adapter for this controller
201 * @dev: device for this controller
202 * @base: virtual memory area
203 * @complete: completion of I2C message
204 * @clk: hw i2c clock
205 * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+
206 * @msg: Pointer to data to be written
207 * @msg_num: number of I2C messages to be executed
208 * @msg_id: message identifiant
209 * @f7_msg: customized i2c msg for driver usage
210 * @setup: I2C timing input setup
211 * @timing: I2C computed timings
212 */
213struct stm32f7_i2c_dev {
214 struct i2c_adapter adap;
215 struct device *dev;
216 void __iomem *base;
217 struct completion complete;
218 struct clk *clk;
219 int speed;
220 struct i2c_msg *msg;
221 unsigned int msg_num;
222 unsigned int msg_id;
223 struct stm32f7_i2c_msg f7_msg;
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200224 struct stm32f7_i2c_setup setup;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200225 struct stm32f7_i2c_timings timing;
226};
227
228/**
229 * All these values are coming from I2C Specification, Version 6.0, 4th of
230 * April 2014.
231 *
232 * Table10. Characteristics of the SDA and SCL bus lines for Standard, Fast,
233 * and Fast-mode Plus I2C-bus devices
234 */
235static struct stm32f7_i2c_spec i2c_specs[] = {
236 [STM32_I2C_SPEED_STANDARD] = {
237 .rate = 100000,
238 .rate_min = 80000,
239 .rate_max = 100000,
240 .fall_max = 300,
241 .rise_max = 1000,
242 .hddat_min = 0,
243 .vddat_max = 3450,
244 .sudat_min = 250,
245 .l_min = 4700,
246 .h_min = 4000,
247 },
248 [STM32_I2C_SPEED_FAST] = {
249 .rate = 400000,
250 .rate_min = 320000,
251 .rate_max = 400000,
252 .fall_max = 300,
253 .rise_max = 300,
254 .hddat_min = 0,
255 .vddat_max = 900,
256 .sudat_min = 100,
257 .l_min = 1300,
258 .h_min = 600,
259 },
260 [STM32_I2C_SPEED_FAST_PLUS] = {
261 .rate = 1000000,
262 .rate_min = 800000,
263 .rate_max = 1000000,
264 .fall_max = 100,
265 .rise_max = 120,
266 .hddat_min = 0,
267 .vddat_max = 450,
268 .sudat_min = 50,
269 .l_min = 500,
270 .h_min = 260,
271 },
272};
273
Colin Ian King25f2f442017-09-18 09:15:39 +0100274static const struct stm32f7_i2c_setup stm32f7_setup = {
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200275 .rise_time = STM32F7_I2C_RISE_TIME_DEFAULT,
276 .fall_time = STM32F7_I2C_FALL_TIME_DEFAULT,
277 .dnf = STM32F7_I2C_DNF_DEFAULT,
278 .analog_filter = STM32F7_I2C_ANALOG_FILTER_ENABLE,
279};
280
281static inline void stm32f7_i2c_set_bits(void __iomem *reg, u32 mask)
282{
283 writel_relaxed(readl_relaxed(reg) | mask, reg);
284}
285
286static inline void stm32f7_i2c_clr_bits(void __iomem *reg, u32 mask)
287{
288 writel_relaxed(readl_relaxed(reg) & ~mask, reg);
289}
290
291static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev,
292 struct stm32f7_i2c_setup *setup,
293 struct stm32f7_i2c_timings *output)
294{
295 u32 p_prev = STM32F7_PRESC_MAX;
296 u32 i2cclk = DIV_ROUND_CLOSEST(NSEC_PER_SEC,
297 setup->clock_src);
298 u32 i2cbus = DIV_ROUND_CLOSEST(NSEC_PER_SEC,
299 setup->speed_freq);
300 u32 clk_error_prev = i2cbus;
301 u32 tsync;
302 u32 af_delay_min, af_delay_max;
303 u32 dnf_delay;
304 u32 clk_min, clk_max;
305 int sdadel_min, sdadel_max;
306 int scldel_min;
307 struct stm32f7_i2c_timings *v, *_v, *s;
308 struct list_head solutions;
309 u16 p, l, a, h;
310 int ret = 0;
311
312 if (setup->speed >= STM32_I2C_SPEED_END) {
313 dev_err(i2c_dev->dev, "speed out of bound {%d/%d}\n",
314 setup->speed, STM32_I2C_SPEED_END - 1);
315 return -EINVAL;
316 }
317
318 if ((setup->rise_time > i2c_specs[setup->speed].rise_max) ||
319 (setup->fall_time > i2c_specs[setup->speed].fall_max)) {
320 dev_err(i2c_dev->dev,
321 "timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
322 setup->rise_time, i2c_specs[setup->speed].rise_max,
323 setup->fall_time, i2c_specs[setup->speed].fall_max);
324 return -EINVAL;
325 }
326
327 if (setup->dnf > STM32F7_I2C_DNF_MAX) {
328 dev_err(i2c_dev->dev,
329 "DNF out of bound %d/%d\n",
330 setup->dnf, STM32F7_I2C_DNF_MAX);
331 return -EINVAL;
332 }
333
334 if (setup->speed_freq > i2c_specs[setup->speed].rate) {
335 dev_err(i2c_dev->dev, "ERROR: Freq {%d/%d}\n",
336 setup->speed_freq, i2c_specs[setup->speed].rate);
337 return -EINVAL;
338 }
339
340 /* Analog and Digital Filters */
341 af_delay_min =
342 (setup->analog_filter ?
343 STM32F7_I2C_ANALOG_FILTER_DELAY_MIN : 0);
344 af_delay_max =
345 (setup->analog_filter ?
346 STM32F7_I2C_ANALOG_FILTER_DELAY_MAX : 0);
347 dnf_delay = setup->dnf * i2cclk;
348
349 sdadel_min = setup->fall_time - i2c_specs[setup->speed].hddat_min -
350 af_delay_min - (setup->dnf + 3) * i2cclk;
351
352 sdadel_max = i2c_specs[setup->speed].vddat_max - setup->rise_time -
353 af_delay_max - (setup->dnf + 4) * i2cclk;
354
355 scldel_min = setup->rise_time + i2c_specs[setup->speed].sudat_min;
356
357 if (sdadel_min < 0)
358 sdadel_min = 0;
359 if (sdadel_max < 0)
360 sdadel_max = 0;
361
362 dev_dbg(i2c_dev->dev, "SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n",
363 sdadel_min, sdadel_max, scldel_min);
364
365 INIT_LIST_HEAD(&solutions);
366 /* Compute possible values for PRESC, SCLDEL and SDADEL */
367 for (p = 0; p < STM32F7_PRESC_MAX; p++) {
368 for (l = 0; l < STM32F7_SCLDEL_MAX; l++) {
369 u32 scldel = (l + 1) * (p + 1) * i2cclk;
370
371 if (scldel < scldel_min)
372 continue;
373
374 for (a = 0; a < STM32F7_SDADEL_MAX; a++) {
375 u32 sdadel = (a * (p + 1) + 1) * i2cclk;
376
377 if (((sdadel >= sdadel_min) &&
378 (sdadel <= sdadel_max)) &&
379 (p != p_prev)) {
380 v = kmalloc(sizeof(*v), GFP_KERNEL);
381 if (!v) {
382 ret = -ENOMEM;
383 goto exit;
384 }
385
386 v->presc = p;
387 v->scldel = l;
388 v->sdadel = a;
389 p_prev = p;
390
391 list_add_tail(&v->node,
392 &solutions);
393 }
394 }
395 }
396 }
397
398 if (list_empty(&solutions)) {
399 dev_err(i2c_dev->dev, "no Prescaler solution\n");
400 ret = -EPERM;
401 goto exit;
402 }
403
404 tsync = af_delay_min + dnf_delay + (2 * i2cclk);
405 s = NULL;
406 clk_max = NSEC_PER_SEC / i2c_specs[setup->speed].rate_min;
407 clk_min = NSEC_PER_SEC / i2c_specs[setup->speed].rate_max;
408
409 /*
410 * Among Prescaler possibilities discovered above figures out SCL Low
411 * and High Period. Provided:
412 * - SCL Low Period has to be higher than SCL Clock Low Period
413 * defined by I2C Specification. I2C Clock has to be lower than
414 * (SCL Low Period - Analog/Digital filters) / 4.
415 * - SCL High Period has to be lower than SCL Clock High Period
416 * defined by I2C Specification
417 * - I2C Clock has to be lower than SCL High Period
418 */
419 list_for_each_entry(v, &solutions, node) {
420 u32 prescaler = (v->presc + 1) * i2cclk;
421
422 for (l = 0; l < STM32F7_SCLL_MAX; l++) {
423 u32 tscl_l = (l + 1) * prescaler + tsync;
424
425 if ((tscl_l < i2c_specs[setup->speed].l_min) ||
426 (i2cclk >=
427 ((tscl_l - af_delay_min - dnf_delay) / 4))) {
428 continue;
429 }
430
431 for (h = 0; h < STM32F7_SCLH_MAX; h++) {
432 u32 tscl_h = (h + 1) * prescaler + tsync;
433 u32 tscl = tscl_l + tscl_h +
434 setup->rise_time + setup->fall_time;
435
436 if ((tscl >= clk_min) && (tscl <= clk_max) &&
437 (tscl_h >= i2c_specs[setup->speed].h_min) &&
438 (i2cclk < tscl_h)) {
439 int clk_error = tscl - i2cbus;
440
441 if (clk_error < 0)
442 clk_error = -clk_error;
443
444 if (clk_error < clk_error_prev) {
445 clk_error_prev = clk_error;
446 v->scll = l;
447 v->sclh = h;
448 s = v;
449 }
450 }
451 }
452 }
453 }
454
455 if (!s) {
456 dev_err(i2c_dev->dev, "no solution at all\n");
457 ret = -EPERM;
458 goto exit;
459 }
460
461 output->presc = s->presc;
462 output->scldel = s->scldel;
463 output->sdadel = s->sdadel;
464 output->scll = s->scll;
465 output->sclh = s->sclh;
466
467 dev_dbg(i2c_dev->dev,
468 "Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
469 output->presc,
470 output->scldel, output->sdadel,
471 output->scll, output->sclh);
472
473exit:
474 /* Release list and memory */
475 list_for_each_entry_safe(v, _v, &solutions, node) {
476 list_del(&v->node);
477 kfree(v);
478 }
479
480 return ret;
481}
482
483static int stm32f7_i2c_setup_timing(struct stm32f7_i2c_dev *i2c_dev,
484 struct stm32f7_i2c_setup *setup)
485{
486 int ret = 0;
487
488 setup->speed = i2c_dev->speed;
489 setup->speed_freq = i2c_specs[setup->speed].rate;
490 setup->clock_src = clk_get_rate(i2c_dev->clk);
491
492 if (!setup->clock_src) {
493 dev_err(i2c_dev->dev, "clock rate is 0\n");
494 return -EINVAL;
495 }
496
497 do {
498 ret = stm32f7_i2c_compute_timing(i2c_dev, setup,
499 &i2c_dev->timing);
500 if (ret) {
501 dev_err(i2c_dev->dev,
502 "failed to compute I2C timings.\n");
503 if (i2c_dev->speed > STM32_I2C_SPEED_STANDARD) {
504 i2c_dev->speed--;
505 setup->speed = i2c_dev->speed;
506 setup->speed_freq =
507 i2c_specs[setup->speed].rate;
508 dev_warn(i2c_dev->dev,
509 "downgrade I2C Speed Freq to (%i)\n",
510 i2c_specs[setup->speed].rate);
511 } else {
512 break;
513 }
514 }
515 } while (ret);
516
517 if (ret) {
518 dev_err(i2c_dev->dev, "Impossible to compute I2C timings.\n");
519 return ret;
520 }
521
522 dev_dbg(i2c_dev->dev, "I2C Speed(%i), Freq(%i), Clk Source(%i)\n",
523 setup->speed, setup->speed_freq, setup->clock_src);
524 dev_dbg(i2c_dev->dev, "I2C Rise(%i) and Fall(%i) Time\n",
525 setup->rise_time, setup->fall_time);
526 dev_dbg(i2c_dev->dev, "I2C Analog Filter(%s), DNF(%i)\n",
527 (setup->analog_filter ? "On" : "Off"), setup->dnf);
528
529 return 0;
530}
531
532static void stm32f7_i2c_hw_config(struct stm32f7_i2c_dev *i2c_dev)
533{
534 struct stm32f7_i2c_timings *t = &i2c_dev->timing;
535 u32 timing = 0;
536
537 /* Timing settings */
538 timing |= STM32F7_I2C_TIMINGR_PRESC(t->presc);
539 timing |= STM32F7_I2C_TIMINGR_SCLDEL(t->scldel);
540 timing |= STM32F7_I2C_TIMINGR_SDADEL(t->sdadel);
541 timing |= STM32F7_I2C_TIMINGR_SCLH(t->sclh);
542 timing |= STM32F7_I2C_TIMINGR_SCLL(t->scll);
543 writel_relaxed(timing, i2c_dev->base + STM32F7_I2C_TIMINGR);
544
545 /* Enable I2C */
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200546 if (i2c_dev->setup.analog_filter)
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200547 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1,
548 STM32F7_I2C_CR1_ANFOFF);
549 else
550 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1,
551 STM32F7_I2C_CR1_ANFOFF);
552 stm32f7_i2c_set_bits(i2c_dev->base + STM32F7_I2C_CR1,
553 STM32F7_I2C_CR1_PE);
554}
555
556static void stm32f7_i2c_write_tx_data(struct stm32f7_i2c_dev *i2c_dev)
557{
558 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
559 void __iomem *base = i2c_dev->base;
560
561 if (f7_msg->count) {
562 writeb_relaxed(*f7_msg->buf++, base + STM32F7_I2C_TXDR);
563 f7_msg->count--;
564 }
565}
566
567static void stm32f7_i2c_read_rx_data(struct stm32f7_i2c_dev *i2c_dev)
568{
569 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
570 void __iomem *base = i2c_dev->base;
571
572 if (f7_msg->count) {
573 *f7_msg->buf++ = readb_relaxed(base + STM32F7_I2C_RXDR);
574 f7_msg->count--;
575 }
576}
577
578static void stm32f7_i2c_reload(struct stm32f7_i2c_dev *i2c_dev)
579{
580 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
581 u32 cr2;
582
583 cr2 = readl_relaxed(i2c_dev->base + STM32F7_I2C_CR2);
584
585 cr2 &= ~STM32F7_I2C_CR2_NBYTES_MASK;
586 if (f7_msg->count > STM32F7_I2C_MAX_LEN) {
587 cr2 |= STM32F7_I2C_CR2_NBYTES(STM32F7_I2C_MAX_LEN);
588 } else {
589 cr2 &= ~STM32F7_I2C_CR2_RELOAD;
590 cr2 |= STM32F7_I2C_CR2_NBYTES(f7_msg->count);
591 }
592
593 writel_relaxed(cr2, i2c_dev->base + STM32F7_I2C_CR2);
594}
595
596static int stm32f7_i2c_wait_free_bus(struct stm32f7_i2c_dev *i2c_dev)
597{
598 u32 status;
599 int ret;
600
601 ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F7_I2C_ISR,
602 status,
603 !(status & STM32F7_I2C_ISR_BUSY),
604 10, 1000);
605 if (ret) {
606 dev_dbg(i2c_dev->dev, "bus busy\n");
607 ret = -EBUSY;
608 }
609
610 return ret;
611}
612
613static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
614 struct i2c_msg *msg)
615{
616 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
617 void __iomem *base = i2c_dev->base;
618 u32 cr1, cr2;
619
620 f7_msg->addr = msg->addr;
621 f7_msg->buf = msg->buf;
622 f7_msg->count = msg->len;
623 f7_msg->result = 0;
624 f7_msg->stop = (i2c_dev->msg_id >= i2c_dev->msg_num - 1);
625
626 reinit_completion(&i2c_dev->complete);
627
628 cr1 = readl_relaxed(base + STM32F7_I2C_CR1);
629 cr2 = readl_relaxed(base + STM32F7_I2C_CR2);
630
631 /* Set transfer direction */
632 cr2 &= ~STM32F7_I2C_CR2_RD_WRN;
633 if (msg->flags & I2C_M_RD)
634 cr2 |= STM32F7_I2C_CR2_RD_WRN;
635
636 /* Set slave address */
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +0200637 cr2 &= ~(STM32F7_I2C_CR2_HEAD10R | STM32F7_I2C_CR2_ADD10);
638 if (msg->flags & I2C_M_TEN) {
639 cr2 &= ~STM32F7_I2C_CR2_SADD10_MASK;
640 cr2 |= STM32F7_I2C_CR2_SADD10(f7_msg->addr);
641 cr2 |= STM32F7_I2C_CR2_ADD10;
642 } else {
643 cr2 &= ~STM32F7_I2C_CR2_SADD7_MASK;
644 cr2 |= STM32F7_I2C_CR2_SADD7(f7_msg->addr);
645 }
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200646
647 /* Set nb bytes to transfer and reload if needed */
648 cr2 &= ~(STM32F7_I2C_CR2_NBYTES_MASK | STM32F7_I2C_CR2_RELOAD);
649 if (f7_msg->count > STM32F7_I2C_MAX_LEN) {
650 cr2 |= STM32F7_I2C_CR2_NBYTES(STM32F7_I2C_MAX_LEN);
651 cr2 |= STM32F7_I2C_CR2_RELOAD;
652 } else {
653 cr2 |= STM32F7_I2C_CR2_NBYTES(f7_msg->count);
654 }
655
656 /* Enable NACK, STOP, error and transfer complete interrupts */
657 cr1 |= STM32F7_I2C_CR1_ERRIE | STM32F7_I2C_CR1_TCIE |
658 STM32F7_I2C_CR1_STOPIE | STM32F7_I2C_CR1_NACKIE;
659
660 /* Clear TX/RX interrupt */
661 cr1 &= ~(STM32F7_I2C_CR1_RXIE | STM32F7_I2C_CR1_TXIE);
662
663 /* Enable RX/TX interrupt according to msg direction */
664 if (msg->flags & I2C_M_RD)
665 cr1 |= STM32F7_I2C_CR1_RXIE;
666 else
667 cr1 |= STM32F7_I2C_CR1_TXIE;
668
669 /* Configure Start/Repeated Start */
670 cr2 |= STM32F7_I2C_CR2_START;
671
672 /* Write configurations registers */
673 writel_relaxed(cr1, base + STM32F7_I2C_CR1);
674 writel_relaxed(cr2, base + STM32F7_I2C_CR2);
675}
676
677static void stm32f7_i2c_disable_irq(struct stm32f7_i2c_dev *i2c_dev, u32 mask)
678{
679 stm32f7_i2c_clr_bits(i2c_dev->base + STM32F7_I2C_CR1, mask);
680}
681
682static irqreturn_t stm32f7_i2c_isr_event(int irq, void *data)
683{
684 struct stm32f7_i2c_dev *i2c_dev = data;
685 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
686 void __iomem *base = i2c_dev->base;
687 u32 status, mask;
688
689 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR);
690
691 /* Tx empty */
692 if (status & STM32F7_I2C_ISR_TXIS)
693 stm32f7_i2c_write_tx_data(i2c_dev);
694
695 /* RX not empty */
696 if (status & STM32F7_I2C_ISR_RXNE)
697 stm32f7_i2c_read_rx_data(i2c_dev);
698
699 /* NACK received */
700 if (status & STM32F7_I2C_ISR_NACKF) {
701 dev_dbg(i2c_dev->dev, "<%s>: Receive NACK\n", __func__);
702 writel_relaxed(STM32F7_I2C_ICR_NACKCF, base + STM32F7_I2C_ICR);
703 f7_msg->result = -ENXIO;
704 }
705
706 /* STOP detection flag */
707 if (status & STM32F7_I2C_ISR_STOPF) {
708 /* Disable interrupts */
709 stm32f7_i2c_disable_irq(i2c_dev, STM32F7_I2C_ALL_IRQ_MASK);
710
711 /* Clear STOP flag */
712 writel_relaxed(STM32F7_I2C_ICR_STOPCF, base + STM32F7_I2C_ICR);
713
714 complete(&i2c_dev->complete);
715 }
716
717 /* Transfer complete */
718 if (status & STM32F7_I2C_ISR_TC) {
719 if (f7_msg->stop) {
720 mask = STM32F7_I2C_CR2_STOP;
721 stm32f7_i2c_set_bits(base + STM32F7_I2C_CR2, mask);
722 } else {
723 i2c_dev->msg_id++;
724 i2c_dev->msg++;
725 stm32f7_i2c_xfer_msg(i2c_dev, i2c_dev->msg);
726 }
727 }
728
729 /*
730 * Transfer Complete Reload: 255 data bytes have been transferred
731 * We have to prepare the I2C controller to transfer the remaining
732 * data.
733 */
734 if (status & STM32F7_I2C_ISR_TCR)
735 stm32f7_i2c_reload(i2c_dev);
736
737 return IRQ_HANDLED;
738}
739
740static irqreturn_t stm32f7_i2c_isr_error(int irq, void *data)
741{
742 struct stm32f7_i2c_dev *i2c_dev = data;
743 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
744 void __iomem *base = i2c_dev->base;
745 struct device *dev = i2c_dev->dev;
746 u32 status;
747
748 status = readl_relaxed(i2c_dev->base + STM32F7_I2C_ISR);
749
750 /* Bus error */
751 if (status & STM32F7_I2C_ISR_BERR) {
752 dev_err(dev, "<%s>: Bus error\n", __func__);
753 writel_relaxed(STM32F7_I2C_ICR_BERRCF, base + STM32F7_I2C_ICR);
754 f7_msg->result = -EIO;
755 }
756
757 /* Arbitration loss */
758 if (status & STM32F7_I2C_ISR_ARLO) {
759 dev_dbg(dev, "<%s>: Arbitration loss\n", __func__);
760 writel_relaxed(STM32F7_I2C_ICR_ARLOCF, base + STM32F7_I2C_ICR);
761 f7_msg->result = -EAGAIN;
762 }
763
764 stm32f7_i2c_disable_irq(i2c_dev, STM32F7_I2C_ALL_IRQ_MASK);
765
766 complete(&i2c_dev->complete);
767
768 return IRQ_HANDLED;
769}
770
771static int stm32f7_i2c_xfer(struct i2c_adapter *i2c_adap,
772 struct i2c_msg msgs[], int num)
773{
774 struct stm32f7_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
775 struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
776 unsigned long time_left;
777 int ret;
778
779 i2c_dev->msg = msgs;
780 i2c_dev->msg_num = num;
781 i2c_dev->msg_id = 0;
782
783 ret = clk_enable(i2c_dev->clk);
784 if (ret) {
785 dev_err(i2c_dev->dev, "Failed to enable clock\n");
786 return ret;
787 }
788
789 ret = stm32f7_i2c_wait_free_bus(i2c_dev);
790 if (ret)
791 goto clk_free;
792
793 stm32f7_i2c_xfer_msg(i2c_dev, msgs);
794
795 time_left = wait_for_completion_timeout(&i2c_dev->complete,
796 i2c_dev->adap.timeout);
797 ret = f7_msg->result;
798
799 if (!time_left) {
800 dev_dbg(i2c_dev->dev, "Access to slave 0x%x timed out\n",
801 i2c_dev->msg->addr);
802 ret = -ETIMEDOUT;
803 }
804
805clk_free:
806 clk_disable(i2c_dev->clk);
807
808 return (ret < 0) ? ret : num;
809}
810
811static u32 stm32f7_i2c_func(struct i2c_adapter *adap)
812{
Pierre-Yves MORDRET8c7ecc92018-04-11 15:24:53 +0200813 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200814}
815
816static struct i2c_algorithm stm32f7_i2c_algo = {
817 .master_xfer = stm32f7_i2c_xfer,
818 .functionality = stm32f7_i2c_func,
819};
820
821static int stm32f7_i2c_probe(struct platform_device *pdev)
822{
823 struct device_node *np = pdev->dev.of_node;
824 struct stm32f7_i2c_dev *i2c_dev;
825 const struct stm32f7_i2c_setup *setup;
826 struct resource *res;
827 u32 irq_error, irq_event, clk_rate, rise_time, fall_time;
828 struct i2c_adapter *adap;
829 struct reset_control *rst;
830 int ret;
831
832 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
833 if (!i2c_dev)
834 return -ENOMEM;
835
836 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
837 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
838 if (IS_ERR(i2c_dev->base))
839 return PTR_ERR(i2c_dev->base);
840
841 irq_event = irq_of_parse_and_map(np, 0);
842 if (!irq_event) {
843 dev_err(&pdev->dev, "IRQ event missing or invalid\n");
844 return -EINVAL;
845 }
846
847 irq_error = irq_of_parse_and_map(np, 1);
848 if (!irq_error) {
849 dev_err(&pdev->dev, "IRQ error missing or invalid\n");
850 return -EINVAL;
851 }
852
853 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
854 if (IS_ERR(i2c_dev->clk)) {
855 dev_err(&pdev->dev, "Error: Missing controller clock\n");
856 return PTR_ERR(i2c_dev->clk);
857 }
858 ret = clk_prepare_enable(i2c_dev->clk);
859 if (ret) {
860 dev_err(&pdev->dev, "Failed to prepare_enable clock\n");
861 return ret;
862 }
863
864 i2c_dev->speed = STM32_I2C_SPEED_STANDARD;
865 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
866 &clk_rate);
867 if (!ret && clk_rate >= 1000000)
868 i2c_dev->speed = STM32_I2C_SPEED_FAST_PLUS;
869 else if (!ret && clk_rate >= 400000)
870 i2c_dev->speed = STM32_I2C_SPEED_FAST;
871 else if (!ret && clk_rate >= 100000)
872 i2c_dev->speed = STM32_I2C_SPEED_STANDARD;
873
874 rst = devm_reset_control_get(&pdev->dev, NULL);
875 if (IS_ERR(rst)) {
876 dev_err(&pdev->dev, "Error: Missing controller reset\n");
877 ret = PTR_ERR(rst);
878 goto clk_free;
879 }
880 reset_control_assert(rst);
881 udelay(2);
882 reset_control_deassert(rst);
883
884 i2c_dev->dev = &pdev->dev;
885
886 ret = devm_request_irq(&pdev->dev, irq_event, stm32f7_i2c_isr_event, 0,
887 pdev->name, i2c_dev);
888 if (ret) {
889 dev_err(&pdev->dev, "Failed to request irq event %i\n",
890 irq_event);
891 goto clk_free;
892 }
893
894 ret = devm_request_irq(&pdev->dev, irq_error, stm32f7_i2c_isr_error, 0,
895 pdev->name, i2c_dev);
896 if (ret) {
897 dev_err(&pdev->dev, "Failed to request irq error %i\n",
898 irq_error);
899 goto clk_free;
900 }
901
902 setup = of_device_get_match_data(&pdev->dev);
Pierre-Yves MORDRET771b7bf2018-03-21 17:48:40 +0100903 if (!setup) {
904 dev_err(&pdev->dev, "Can't get device data\n");
905 ret = -ENODEV;
906 goto clk_free;
907 }
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200908 i2c_dev->setup = *setup;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200909
910 ret = device_property_read_u32(i2c_dev->dev, "i2c-scl-rising-time-ns",
911 &rise_time);
912 if (!ret)
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200913 i2c_dev->setup.rise_time = rise_time;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200914
915 ret = device_property_read_u32(i2c_dev->dev, "i2c-scl-falling-time-ns",
916 &fall_time);
917 if (!ret)
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200918 i2c_dev->setup.fall_time = fall_time;
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200919
Pierre-Yves MORDRET463a9212017-09-21 15:30:09 +0200920 ret = stm32f7_i2c_setup_timing(i2c_dev, &i2c_dev->setup);
Pierre-Yves MORDRETaeb068c2017-09-14 16:28:37 +0200921 if (ret)
922 goto clk_free;
923
924 stm32f7_i2c_hw_config(i2c_dev);
925
926 adap = &i2c_dev->adap;
927 i2c_set_adapdata(adap, i2c_dev);
928 snprintf(adap->name, sizeof(adap->name), "STM32F7 I2C(%pa)",
929 &res->start);
930 adap->owner = THIS_MODULE;
931 adap->timeout = 2 * HZ;
932 adap->retries = 3;
933 adap->algo = &stm32f7_i2c_algo;
934 adap->dev.parent = &pdev->dev;
935 adap->dev.of_node = pdev->dev.of_node;
936
937 init_completion(&i2c_dev->complete);
938
939 ret = i2c_add_adapter(adap);
940 if (ret)
941 goto clk_free;
942
943 platform_set_drvdata(pdev, i2c_dev);
944
945 clk_disable(i2c_dev->clk);
946
947 dev_info(i2c_dev->dev, "STM32F7 I2C-%d bus adapter\n", adap->nr);
948
949 return 0;
950
951clk_free:
952 clk_disable_unprepare(i2c_dev->clk);
953
954 return ret;
955}
956
957static int stm32f7_i2c_remove(struct platform_device *pdev)
958{
959 struct stm32f7_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
960
961 i2c_del_adapter(&i2c_dev->adap);
962
963 clk_unprepare(i2c_dev->clk);
964
965 return 0;
966}
967
968static const struct of_device_id stm32f7_i2c_match[] = {
969 { .compatible = "st,stm32f7-i2c", .data = &stm32f7_setup},
970 {},
971};
972MODULE_DEVICE_TABLE(of, stm32f7_i2c_match);
973
974static struct platform_driver stm32f7_i2c_driver = {
975 .driver = {
976 .name = "stm32f7-i2c",
977 .of_match_table = stm32f7_i2c_match,
978 },
979 .probe = stm32f7_i2c_probe,
980 .remove = stm32f7_i2c_remove,
981};
982
983module_platform_driver(stm32f7_i2c_driver);
984
985MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
986MODULE_DESCRIPTION("STMicroelectronics STM32F7 I2C driver");
987MODULE_LICENSE("GPL v2");