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 .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_DEPRECATED
,
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 ACPI_COMPANION_SET(&i2c
->adap
.dev
, ACPI_COMPANION(&pdev
->dev
));
306 i2c_set_adapdata(&i2c
->adap
, i2c
);
307 platform_set_drvdata(pdev
, i2c
);
309 kempld_get_mutex(pld
);
310 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
312 if (ctrl
& I2C_CTRL_EN
)
313 i2c
->was_active
= true;
315 kempld_i2c_device_init(i2c
);
316 kempld_release_mutex(pld
);
318 /* Add I2C adapter to I2C tree */
320 i2c
->adap
.nr
= i2c_bus
;
321 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
325 dev_info(i2c
->dev
, "I2C bus initialized at %dkHz\n",
331 static void kempld_i2c_remove(struct platform_device
*pdev
)
333 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
334 struct kempld_device_data
*pld
= i2c
->pld
;
337 kempld_get_mutex(pld
);
339 * Disable I2C logic if it was not activated before the
342 if (!i2c
->was_active
) {
343 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
344 ctrl
&= ~I2C_CTRL_EN
;
345 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
347 kempld_release_mutex(pld
);
349 i2c_del_adapter(&i2c
->adap
);
352 static int kempld_i2c_suspend(struct device
*dev
)
354 struct kempld_i2c_data
*i2c
= dev_get_drvdata(dev
);
355 struct kempld_device_data
*pld
= i2c
->pld
;
358 kempld_get_mutex(pld
);
359 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
360 ctrl
&= ~I2C_CTRL_EN
;
361 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
362 kempld_release_mutex(pld
);
367 static int kempld_i2c_resume(struct device
*dev
)
369 struct kempld_i2c_data
*i2c
= dev_get_drvdata(dev
);
370 struct kempld_device_data
*pld
= i2c
->pld
;
372 kempld_get_mutex(pld
);
373 kempld_i2c_device_init(i2c
);
374 kempld_release_mutex(pld
);
379 static DEFINE_SIMPLE_DEV_PM_OPS(kempld_i2c_pm_ops
,
380 kempld_i2c_suspend
, kempld_i2c_resume
);
382 static struct platform_driver kempld_i2c_driver
= {
384 .name
= "kempld-i2c",
385 .pm
= pm_sleep_ptr(&kempld_i2c_pm_ops
),
387 .probe
= kempld_i2c_probe
,
388 .remove_new
= kempld_i2c_remove
,
391 module_platform_driver(kempld_i2c_driver
);
393 MODULE_DESCRIPTION("KEM PLD I2C Driver");
394 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
395 MODULE_LICENSE("GPL");
396 MODULE_ALIAS("platform:kempld_i2c");