1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C bus driver for Kontron COM modules
5 * Copyright (c) 2010-2013 Kontron Europe GmbH
6 * Author: Michael Brunner <michael.brunner@kontron.com>
8 * The driver is based on the i2c-ocores driver by Peter Korsgaard.
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/mfd/kempld.h>
17 #define KEMPLD_I2C_PRELOW 0x0b
18 #define KEMPLD_I2C_PREHIGH 0x0c
19 #define KEMPLD_I2C_DATA 0x0e
21 #define KEMPLD_I2C_CTRL 0x0d
22 #define I2C_CTRL_IEN 0x40
23 #define I2C_CTRL_EN 0x80
25 #define KEMPLD_I2C_STAT 0x0f
26 #define I2C_STAT_IF 0x01
27 #define I2C_STAT_TIP 0x02
28 #define I2C_STAT_ARBLOST 0x20
29 #define I2C_STAT_BUSY 0x40
30 #define I2C_STAT_NACK 0x80
32 #define KEMPLD_I2C_CMD 0x0f
33 #define I2C_CMD_START 0x91
34 #define I2C_CMD_STOP 0x41
35 #define I2C_CMD_READ 0x21
36 #define I2C_CMD_WRITE 0x11
37 #define I2C_CMD_READ_ACK 0x21
38 #define I2C_CMD_READ_NACK 0x29
39 #define I2C_CMD_IACK 0x01
41 #define KEMPLD_I2C_FREQ_MAX 2700 /* 2.7 mHz */
42 #define KEMPLD_I2C_FREQ_STD 100 /* 100 kHz */
55 struct kempld_i2c_data
{
57 struct kempld_device_data
*pld
;
58 struct i2c_adapter adap
;
66 static unsigned int bus_frequency
= KEMPLD_I2C_FREQ_STD
;
67 module_param(bus_frequency
, uint
, 0);
68 MODULE_PARM_DESC(bus_frequency
, "Set I2C bus frequency in kHz (default="
69 __MODULE_STRING(KEMPLD_I2C_FREQ_STD
)")");
71 static int i2c_bus
= -1;
72 module_param(i2c_bus
, int, 0);
73 MODULE_PARM_DESC(i2c_bus
, "Set I2C bus number (default=-1 for dynamic assignment)");
75 static bool i2c_gpio_mux
;
76 module_param(i2c_gpio_mux
, bool, 0);
77 MODULE_PARM_DESC(i2c_gpio_mux
, "Enable I2C port on GPIO out (default=false)");
80 * kempld_get_mutex must be called prior to calling this function.
82 static int kempld_i2c_process(struct kempld_i2c_data
*i2c
)
84 struct kempld_device_data
*pld
= i2c
->pld
;
85 u8 stat
= kempld_read8(pld
, KEMPLD_I2C_STAT
);
86 struct i2c_msg
*msg
= i2c
->msg
;
90 if (stat
& I2C_STAT_TIP
)
93 if (i2c
->state
== STATE_DONE
|| i2c
->state
== STATE_ERROR
) {
94 /* Stop has been sent */
95 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_IACK
);
96 if (i2c
->state
== STATE_ERROR
)
102 if (stat
& I2C_STAT_ARBLOST
) {
103 i2c
->state
= STATE_ERROR
;
104 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
108 if (i2c
->state
== STATE_INIT
) {
109 if (stat
& I2C_STAT_BUSY
)
112 i2c
->state
= STATE_ADDR
;
115 if (i2c
->state
== STATE_ADDR
) {
116 /* 10 bit address? */
117 if (i2c
->msg
->flags
& I2C_M_TEN
) {
118 addr
= 0xf0 | ((i2c
->msg
->addr
>> 7) & 0x6);
119 /* Set read bit if necessary */
120 addr
|= (i2c
->msg
->flags
& I2C_M_RD
) ? 1 : 0;
121 i2c
->state
= STATE_ADDR10
;
123 addr
= i2c_8bit_addr_from_msg(i2c
->msg
);
124 i2c
->state
= STATE_START
;
127 kempld_write8(pld
, KEMPLD_I2C_DATA
, addr
);
128 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_START
);
133 /* Second part of 10 bit addressing */
134 if (i2c
->state
== STATE_ADDR10
) {
135 kempld_write8(pld
, KEMPLD_I2C_DATA
, i2c
->msg
->addr
& 0xff);
136 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
138 i2c
->state
= STATE_START
;
142 if (i2c
->state
== STATE_START
|| i2c
->state
== STATE_WRITE
) {
143 i2c
->state
= (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
145 if (stat
& I2C_STAT_NACK
) {
146 i2c
->state
= STATE_ERROR
;
147 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
151 msg
->buf
[i2c
->pos
++] = kempld_read8(pld
, KEMPLD_I2C_DATA
);
154 if (i2c
->pos
>= msg
->len
) {
161 if (!(msg
->flags
& I2C_M_NOSTART
)) {
162 i2c
->state
= STATE_ADDR
;
165 i2c
->state
= (msg
->flags
& I2C_M_RD
)
166 ? STATE_READ
: STATE_WRITE
;
169 i2c
->state
= STATE_DONE
;
170 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
175 if (i2c
->state
== STATE_READ
) {
176 kempld_write8(pld
, KEMPLD_I2C_CMD
, i2c
->pos
== (msg
->len
- 1) ?
177 I2C_CMD_READ_NACK
: I2C_CMD_READ_ACK
);
179 kempld_write8(pld
, KEMPLD_I2C_DATA
, msg
->buf
[i2c
->pos
++]);
180 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
186 static int kempld_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
189 struct kempld_i2c_data
*i2c
= i2c_get_adapdata(adap
);
190 struct kempld_device_data
*pld
= i2c
->pld
;
191 unsigned long timeout
= jiffies
+ HZ
;
197 i2c
->state
= STATE_INIT
;
199 /* Handle the transfer */
200 while (time_before(jiffies
, timeout
)) {
201 kempld_get_mutex(pld
);
202 ret
= kempld_i2c_process(i2c
);
203 kempld_release_mutex(pld
);
205 if (i2c
->state
== STATE_DONE
|| i2c
->state
== STATE_ERROR
)
206 return (i2c
->state
== STATE_DONE
) ? num
: ret
;
209 timeout
= jiffies
+ HZ
;
214 i2c
->state
= STATE_ERROR
;
220 * kempld_get_mutex must be called prior to calling this function.
222 static void kempld_i2c_device_init(struct kempld_i2c_data
*i2c
)
224 struct kempld_device_data
*pld
= i2c
->pld
;
231 /* Make sure the device is disabled */
232 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
233 ctrl
&= ~(I2C_CTRL_EN
| I2C_CTRL_IEN
);
234 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
236 if (bus_frequency
> KEMPLD_I2C_FREQ_MAX
)
237 bus_frequency
= KEMPLD_I2C_FREQ_MAX
;
239 if (pld
->info
.spec_major
== 1)
240 prescale
= pld
->pld_clock
/ (bus_frequency
* 5) - 1000;
242 prescale
= pld
->pld_clock
/ (bus_frequency
* 4) - 3000;
247 /* Round to the best matching value */
248 prescale_corr
= prescale
/ 1000;
249 if (prescale
% 1000 >= 500)
252 kempld_write8(pld
, KEMPLD_I2C_PRELOW
, prescale_corr
& 0xff);
253 kempld_write8(pld
, KEMPLD_I2C_PREHIGH
, prescale_corr
>> 8);
255 /* Activate I2C bus output on GPIO pins */
256 cfg
= kempld_read8(pld
, KEMPLD_CFG
);
258 cfg
|= KEMPLD_CFG_GPIO_I2C_MUX
;
260 cfg
&= ~KEMPLD_CFG_GPIO_I2C_MUX
;
261 kempld_write8(pld
, KEMPLD_CFG
, cfg
);
263 /* Enable the device */
264 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_IACK
);
266 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
268 stat
= kempld_read8(pld
, KEMPLD_I2C_STAT
);
269 if (stat
& I2C_STAT_BUSY
)
270 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
273 static u32
kempld_i2c_func(struct i2c_adapter
*adap
)
275 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
| I2C_FUNC_SMBUS_EMUL
;
278 static const struct i2c_algorithm kempld_i2c_algorithm
= {
279 .master_xfer
= kempld_i2c_xfer
,
280 .functionality
= kempld_i2c_func
,
283 static const struct i2c_adapter kempld_i2c_adapter
= {
284 .owner
= THIS_MODULE
,
285 .name
= "i2c-kempld",
286 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
287 .algo
= &kempld_i2c_algorithm
,
290 static int kempld_i2c_probe(struct platform_device
*pdev
)
292 struct kempld_device_data
*pld
= dev_get_drvdata(pdev
->dev
.parent
);
293 struct kempld_i2c_data
*i2c
;
297 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
302 i2c
->dev
= &pdev
->dev
;
303 i2c
->adap
= kempld_i2c_adapter
;
304 i2c
->adap
.dev
.parent
= i2c
->dev
;
305 i2c_set_adapdata(&i2c
->adap
, i2c
);
306 platform_set_drvdata(pdev
, i2c
);
308 kempld_get_mutex(pld
);
309 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
311 if (ctrl
& I2C_CTRL_EN
)
312 i2c
->was_active
= true;
314 kempld_i2c_device_init(i2c
);
315 kempld_release_mutex(pld
);
317 /* Add I2C adapter to I2C tree */
319 i2c
->adap
.nr
= i2c_bus
;
320 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
324 dev_info(i2c
->dev
, "I2C bus initialized at %dkHz\n",
330 static int kempld_i2c_remove(struct platform_device
*pdev
)
332 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
333 struct kempld_device_data
*pld
= i2c
->pld
;
336 kempld_get_mutex(pld
);
338 * Disable I2C logic if it was not activated before the
341 if (!i2c
->was_active
) {
342 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
343 ctrl
&= ~I2C_CTRL_EN
;
344 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
346 kempld_release_mutex(pld
);
348 i2c_del_adapter(&i2c
->adap
);
354 static int kempld_i2c_suspend(struct platform_device
*pdev
, pm_message_t state
)
356 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
357 struct kempld_device_data
*pld
= i2c
->pld
;
360 kempld_get_mutex(pld
);
361 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
362 ctrl
&= ~I2C_CTRL_EN
;
363 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
364 kempld_release_mutex(pld
);
369 static int kempld_i2c_resume(struct platform_device
*pdev
)
371 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
372 struct kempld_device_data
*pld
= i2c
->pld
;
374 kempld_get_mutex(pld
);
375 kempld_i2c_device_init(i2c
);
376 kempld_release_mutex(pld
);
381 #define kempld_i2c_suspend NULL
382 #define kempld_i2c_resume NULL
385 static struct platform_driver kempld_i2c_driver
= {
387 .name
= "kempld-i2c",
389 .probe
= kempld_i2c_probe
,
390 .remove
= kempld_i2c_remove
,
391 .suspend
= kempld_i2c_suspend
,
392 .resume
= kempld_i2c_resume
,
395 module_platform_driver(kempld_i2c_driver
);
397 MODULE_DESCRIPTION("KEM PLD I2C Driver");
398 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
399 MODULE_LICENSE("GPL");
400 MODULE_ALIAS("platform:kempld_i2c");