2 * I2C bus driver for Kontron COM modules
4 * Copyright (c) 2010-2013 Kontron Europe GmbH
5 * Author: Michael Brunner <michael.brunner@kontron.com>
7 * The driver is based on the i2c-ocores driver by Peter Korsgaard.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License 2 as published
11 * by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/mfd/kempld.h>
25 #define KEMPLD_I2C_PRELOW 0x0b
26 #define KEMPLD_I2C_PREHIGH 0x0c
27 #define KEMPLD_I2C_DATA 0x0e
29 #define KEMPLD_I2C_CTRL 0x0d
30 #define I2C_CTRL_IEN 0x40
31 #define I2C_CTRL_EN 0x80
33 #define KEMPLD_I2C_STAT 0x0f
34 #define I2C_STAT_IF 0x01
35 #define I2C_STAT_TIP 0x02
36 #define I2C_STAT_ARBLOST 0x20
37 #define I2C_STAT_BUSY 0x40
38 #define I2C_STAT_NACK 0x80
40 #define KEMPLD_I2C_CMD 0x0f
41 #define I2C_CMD_START 0x91
42 #define I2C_CMD_STOP 0x41
43 #define I2C_CMD_READ 0x21
44 #define I2C_CMD_WRITE 0x11
45 #define I2C_CMD_READ_ACK 0x21
46 #define I2C_CMD_READ_NACK 0x29
47 #define I2C_CMD_IACK 0x01
49 #define KEMPLD_I2C_FREQ_MAX 2700 /* 2.7 mHz */
50 #define KEMPLD_I2C_FREQ_STD 100 /* 100 kHz */
63 struct kempld_i2c_data
{
65 struct kempld_device_data
*pld
;
66 struct i2c_adapter adap
;
74 static unsigned int bus_frequency
= KEMPLD_I2C_FREQ_STD
;
75 module_param(bus_frequency
, uint
, 0);
76 MODULE_PARM_DESC(bus_frequency
, "Set I2C bus frequency in kHz (default="
77 __MODULE_STRING(KEMPLD_I2C_FREQ_STD
)")");
79 static int i2c_bus
= -1;
80 module_param(i2c_bus
, int, 0);
81 MODULE_PARM_DESC(i2c_bus
, "Set I2C bus number (default=-1 for dynamic assignment)");
83 static bool i2c_gpio_mux
;
84 module_param(i2c_gpio_mux
, bool, 0);
85 MODULE_PARM_DESC(i2c_gpio_mux
, "Enable I2C port on GPIO out (default=false)");
88 * kempld_get_mutex must be called prior to calling this function.
90 static int kempld_i2c_process(struct kempld_i2c_data
*i2c
)
92 struct kempld_device_data
*pld
= i2c
->pld
;
93 u8 stat
= kempld_read8(pld
, KEMPLD_I2C_STAT
);
94 struct i2c_msg
*msg
= i2c
->msg
;
98 if (stat
& I2C_STAT_TIP
)
101 if (i2c
->state
== STATE_DONE
|| i2c
->state
== STATE_ERROR
) {
102 /* Stop has been sent */
103 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_IACK
);
104 if (i2c
->state
== STATE_ERROR
)
110 if (stat
& I2C_STAT_ARBLOST
) {
111 i2c
->state
= STATE_ERROR
;
112 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
116 if (i2c
->state
== STATE_INIT
) {
117 if (stat
& I2C_STAT_BUSY
)
120 i2c
->state
= STATE_ADDR
;
123 if (i2c
->state
== STATE_ADDR
) {
124 /* 10 bit address? */
125 if (i2c
->msg
->flags
& I2C_M_TEN
) {
126 addr
= 0xf0 | ((i2c
->msg
->addr
>> 7) & 0x6);
127 /* Set read bit if necessary */
128 addr
|= (i2c
->msg
->flags
& I2C_M_RD
) ? 1 : 0;
129 i2c
->state
= STATE_ADDR10
;
131 addr
= i2c_8bit_addr_from_msg(i2c
->msg
);
132 i2c
->state
= STATE_START
;
135 kempld_write8(pld
, KEMPLD_I2C_DATA
, addr
);
136 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_START
);
141 /* Second part of 10 bit addressing */
142 if (i2c
->state
== STATE_ADDR10
) {
143 kempld_write8(pld
, KEMPLD_I2C_DATA
, i2c
->msg
->addr
& 0xff);
144 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
146 i2c
->state
= STATE_START
;
150 if (i2c
->state
== STATE_START
|| i2c
->state
== STATE_WRITE
) {
151 i2c
->state
= (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
153 if (stat
& I2C_STAT_NACK
) {
154 i2c
->state
= STATE_ERROR
;
155 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
159 msg
->buf
[i2c
->pos
++] = kempld_read8(pld
, KEMPLD_I2C_DATA
);
162 if (i2c
->pos
>= msg
->len
) {
169 if (!(msg
->flags
& I2C_M_NOSTART
)) {
170 i2c
->state
= STATE_ADDR
;
173 i2c
->state
= (msg
->flags
& I2C_M_RD
)
174 ? STATE_READ
: STATE_WRITE
;
177 i2c
->state
= STATE_DONE
;
178 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
183 if (i2c
->state
== STATE_READ
) {
184 kempld_write8(pld
, KEMPLD_I2C_CMD
, i2c
->pos
== (msg
->len
- 1) ?
185 I2C_CMD_READ_NACK
: I2C_CMD_READ_ACK
);
187 kempld_write8(pld
, KEMPLD_I2C_DATA
, msg
->buf
[i2c
->pos
++]);
188 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
194 static int kempld_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
197 struct kempld_i2c_data
*i2c
= i2c_get_adapdata(adap
);
198 struct kempld_device_data
*pld
= i2c
->pld
;
199 unsigned long timeout
= jiffies
+ HZ
;
205 i2c
->state
= STATE_INIT
;
207 /* Handle the transfer */
208 while (time_before(jiffies
, timeout
)) {
209 kempld_get_mutex(pld
);
210 ret
= kempld_i2c_process(i2c
);
211 kempld_release_mutex(pld
);
213 if (i2c
->state
== STATE_DONE
|| i2c
->state
== STATE_ERROR
)
214 return (i2c
->state
== STATE_DONE
) ? num
: ret
;
217 timeout
= jiffies
+ HZ
;
222 i2c
->state
= STATE_ERROR
;
228 * kempld_get_mutex must be called prior to calling this function.
230 static void kempld_i2c_device_init(struct kempld_i2c_data
*i2c
)
232 struct kempld_device_data
*pld
= i2c
->pld
;
239 /* Make sure the device is disabled */
240 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
241 ctrl
&= ~(I2C_CTRL_EN
| I2C_CTRL_IEN
);
242 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
244 if (bus_frequency
> KEMPLD_I2C_FREQ_MAX
)
245 bus_frequency
= KEMPLD_I2C_FREQ_MAX
;
247 if (pld
->info
.spec_major
== 1)
248 prescale
= pld
->pld_clock
/ (bus_frequency
* 5) - 1000;
250 prescale
= pld
->pld_clock
/ (bus_frequency
* 4) - 3000;
255 /* Round to the best matching value */
256 prescale_corr
= prescale
/ 1000;
257 if (prescale
% 1000 >= 500)
260 kempld_write8(pld
, KEMPLD_I2C_PRELOW
, prescale_corr
& 0xff);
261 kempld_write8(pld
, KEMPLD_I2C_PREHIGH
, prescale_corr
>> 8);
263 /* Activate I2C bus output on GPIO pins */
264 cfg
= kempld_read8(pld
, KEMPLD_CFG
);
266 cfg
|= KEMPLD_CFG_GPIO_I2C_MUX
;
268 cfg
&= ~KEMPLD_CFG_GPIO_I2C_MUX
;
269 kempld_write8(pld
, KEMPLD_CFG
, cfg
);
271 /* Enable the device */
272 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_IACK
);
274 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
276 stat
= kempld_read8(pld
, KEMPLD_I2C_STAT
);
277 if (stat
& I2C_STAT_BUSY
)
278 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
281 static u32
kempld_i2c_func(struct i2c_adapter
*adap
)
283 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
| I2C_FUNC_SMBUS_EMUL
;
286 static const struct i2c_algorithm kempld_i2c_algorithm
= {
287 .master_xfer
= kempld_i2c_xfer
,
288 .functionality
= kempld_i2c_func
,
291 static const struct i2c_adapter kempld_i2c_adapter
= {
292 .owner
= THIS_MODULE
,
293 .name
= "i2c-kempld",
294 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
295 .algo
= &kempld_i2c_algorithm
,
298 static int kempld_i2c_probe(struct platform_device
*pdev
)
300 struct kempld_device_data
*pld
= dev_get_drvdata(pdev
->dev
.parent
);
301 struct kempld_i2c_data
*i2c
;
305 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
310 i2c
->dev
= &pdev
->dev
;
311 i2c
->adap
= kempld_i2c_adapter
;
312 i2c
->adap
.dev
.parent
= i2c
->dev
;
313 i2c_set_adapdata(&i2c
->adap
, i2c
);
314 platform_set_drvdata(pdev
, i2c
);
316 kempld_get_mutex(pld
);
317 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
319 if (ctrl
& I2C_CTRL_EN
)
320 i2c
->was_active
= true;
322 kempld_i2c_device_init(i2c
);
323 kempld_release_mutex(pld
);
325 /* Add I2C adapter to I2C tree */
327 i2c
->adap
.nr
= i2c_bus
;
328 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
332 dev_info(i2c
->dev
, "I2C bus initialized at %dkHz\n",
338 static int kempld_i2c_remove(struct platform_device
*pdev
)
340 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
341 struct kempld_device_data
*pld
= i2c
->pld
;
344 kempld_get_mutex(pld
);
346 * Disable I2C logic if it was not activated before the
349 if (!i2c
->was_active
) {
350 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
351 ctrl
&= ~I2C_CTRL_EN
;
352 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
354 kempld_release_mutex(pld
);
356 i2c_del_adapter(&i2c
->adap
);
362 static int kempld_i2c_suspend(struct platform_device
*pdev
, pm_message_t state
)
364 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
365 struct kempld_device_data
*pld
= i2c
->pld
;
368 kempld_get_mutex(pld
);
369 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
370 ctrl
&= ~I2C_CTRL_EN
;
371 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
372 kempld_release_mutex(pld
);
377 static int kempld_i2c_resume(struct platform_device
*pdev
)
379 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
380 struct kempld_device_data
*pld
= i2c
->pld
;
382 kempld_get_mutex(pld
);
383 kempld_i2c_device_init(i2c
);
384 kempld_release_mutex(pld
);
389 #define kempld_i2c_suspend NULL
390 #define kempld_i2c_resume NULL
393 static struct platform_driver kempld_i2c_driver
= {
395 .name
= "kempld-i2c",
397 .probe
= kempld_i2c_probe
,
398 .remove
= kempld_i2c_remove
,
399 .suspend
= kempld_i2c_suspend
,
400 .resume
= kempld_i2c_resume
,
403 module_platform_driver(kempld_i2c_driver
);
405 MODULE_DESCRIPTION("KEM PLD I2C Driver");
406 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
407 MODULE_LICENSE("GPL");
408 MODULE_ALIAS("platform:kempld_i2c");