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 i2c
->state
= STATE_ADDR10
;
129 addr
= (i2c
->msg
->addr
<< 1);
130 i2c
->state
= STATE_START
;
133 /* Set read bit if necessary */
134 addr
|= (i2c
->msg
->flags
& I2C_M_RD
) ? 1 : 0;
136 kempld_write8(pld
, KEMPLD_I2C_DATA
, addr
);
137 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_START
);
142 /* Second part of 10 bit addressing */
143 if (i2c
->state
== STATE_ADDR10
) {
144 kempld_write8(pld
, KEMPLD_I2C_DATA
, i2c
->msg
->addr
& 0xff);
145 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
147 i2c
->state
= STATE_START
;
151 if (i2c
->state
== STATE_START
|| i2c
->state
== STATE_WRITE
) {
152 i2c
->state
= (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
154 if (stat
& I2C_STAT_NACK
) {
155 i2c
->state
= STATE_ERROR
;
156 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
160 msg
->buf
[i2c
->pos
++] = kempld_read8(pld
, KEMPLD_I2C_DATA
);
163 if (i2c
->pos
>= msg
->len
) {
170 if (!(msg
->flags
& I2C_M_NOSTART
)) {
171 i2c
->state
= STATE_ADDR
;
174 i2c
->state
= (msg
->flags
& I2C_M_RD
)
175 ? STATE_READ
: STATE_WRITE
;
178 i2c
->state
= STATE_DONE
;
179 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
184 if (i2c
->state
== STATE_READ
) {
185 kempld_write8(pld
, KEMPLD_I2C_CMD
, i2c
->pos
== (msg
->len
- 1) ?
186 I2C_CMD_READ_NACK
: I2C_CMD_READ_ACK
);
188 kempld_write8(pld
, KEMPLD_I2C_DATA
, msg
->buf
[i2c
->pos
++]);
189 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_WRITE
);
195 static int kempld_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
198 struct kempld_i2c_data
*i2c
= i2c_get_adapdata(adap
);
199 struct kempld_device_data
*pld
= i2c
->pld
;
200 unsigned long timeout
= jiffies
+ HZ
;
206 i2c
->state
= STATE_INIT
;
208 /* Handle the transfer */
209 while (time_before(jiffies
, timeout
)) {
210 kempld_get_mutex(pld
);
211 ret
= kempld_i2c_process(i2c
);
212 kempld_release_mutex(pld
);
214 if (i2c
->state
== STATE_DONE
|| i2c
->state
== STATE_ERROR
)
215 return (i2c
->state
== STATE_DONE
) ? num
: ret
;
218 timeout
= jiffies
+ HZ
;
223 i2c
->state
= STATE_ERROR
;
229 * kempld_get_mutex must be called prior to calling this function.
231 static void kempld_i2c_device_init(struct kempld_i2c_data
*i2c
)
233 struct kempld_device_data
*pld
= i2c
->pld
;
240 /* Make sure the device is disabled */
241 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
242 ctrl
&= ~(I2C_CTRL_EN
| I2C_CTRL_IEN
);
243 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
245 if (bus_frequency
> KEMPLD_I2C_FREQ_MAX
)
246 bus_frequency
= KEMPLD_I2C_FREQ_MAX
;
248 if (pld
->info
.spec_major
== 1)
249 prescale
= pld
->pld_clock
/ (bus_frequency
* 5) - 1000;
251 prescale
= pld
->pld_clock
/ (bus_frequency
* 4) - 3000;
256 /* Round to the best matching value */
257 prescale_corr
= prescale
/ 1000;
258 if (prescale
% 1000 >= 500)
261 kempld_write8(pld
, KEMPLD_I2C_PRELOW
, prescale_corr
& 0xff);
262 kempld_write8(pld
, KEMPLD_I2C_PREHIGH
, prescale_corr
>> 8);
264 /* Activate I2C bus output on GPIO pins */
265 cfg
= kempld_read8(pld
, KEMPLD_CFG
);
267 cfg
|= KEMPLD_CFG_GPIO_I2C_MUX
;
269 cfg
&= ~KEMPLD_CFG_GPIO_I2C_MUX
;
270 kempld_write8(pld
, KEMPLD_CFG
, cfg
);
272 /* Enable the device */
273 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_IACK
);
275 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
277 stat
= kempld_read8(pld
, KEMPLD_I2C_STAT
);
278 if (stat
& I2C_STAT_BUSY
)
279 kempld_write8(pld
, KEMPLD_I2C_CMD
, I2C_CMD_STOP
);
282 static u32
kempld_i2c_func(struct i2c_adapter
*adap
)
284 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
| I2C_FUNC_SMBUS_EMUL
;
287 static const struct i2c_algorithm kempld_i2c_algorithm
= {
288 .master_xfer
= kempld_i2c_xfer
,
289 .functionality
= kempld_i2c_func
,
292 static struct i2c_adapter kempld_i2c_adapter
= {
293 .owner
= THIS_MODULE
,
294 .name
= "i2c-kempld",
295 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
296 .algo
= &kempld_i2c_algorithm
,
299 static int kempld_i2c_probe(struct platform_device
*pdev
)
301 struct kempld_device_data
*pld
= dev_get_drvdata(pdev
->dev
.parent
);
302 struct kempld_i2c_data
*i2c
;
306 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
311 i2c
->dev
= &pdev
->dev
;
312 i2c
->adap
= kempld_i2c_adapter
;
313 i2c
->adap
.dev
.parent
= i2c
->dev
;
314 i2c_set_adapdata(&i2c
->adap
, i2c
);
315 platform_set_drvdata(pdev
, i2c
);
317 kempld_get_mutex(pld
);
318 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
320 if (ctrl
& I2C_CTRL_EN
)
321 i2c
->was_active
= true;
323 kempld_i2c_device_init(i2c
);
324 kempld_release_mutex(pld
);
326 /* Add I2C adapter to I2C tree */
328 i2c
->adap
.nr
= i2c_bus
;
329 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
333 dev_info(i2c
->dev
, "I2C bus initialized at %dkHz\n",
339 static int kempld_i2c_remove(struct platform_device
*pdev
)
341 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
342 struct kempld_device_data
*pld
= i2c
->pld
;
345 kempld_get_mutex(pld
);
347 * Disable I2C logic if it was not activated before the
350 if (!i2c
->was_active
) {
351 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
352 ctrl
&= ~I2C_CTRL_EN
;
353 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
355 kempld_release_mutex(pld
);
357 i2c_del_adapter(&i2c
->adap
);
363 static int kempld_i2c_suspend(struct platform_device
*pdev
, pm_message_t state
)
365 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
366 struct kempld_device_data
*pld
= i2c
->pld
;
369 kempld_get_mutex(pld
);
370 ctrl
= kempld_read8(pld
, KEMPLD_I2C_CTRL
);
371 ctrl
&= ~I2C_CTRL_EN
;
372 kempld_write8(pld
, KEMPLD_I2C_CTRL
, ctrl
);
373 kempld_release_mutex(pld
);
378 static int kempld_i2c_resume(struct platform_device
*pdev
)
380 struct kempld_i2c_data
*i2c
= platform_get_drvdata(pdev
);
381 struct kempld_device_data
*pld
= i2c
->pld
;
383 kempld_get_mutex(pld
);
384 kempld_i2c_device_init(i2c
);
385 kempld_release_mutex(pld
);
390 #define kempld_i2c_suspend NULL
391 #define kempld_i2c_resume NULL
394 static struct platform_driver kempld_i2c_driver
= {
396 .name
= "kempld-i2c",
397 .owner
= THIS_MODULE
,
399 .probe
= kempld_i2c_probe
,
400 .remove
= kempld_i2c_remove
,
401 .suspend
= kempld_i2c_suspend
,
402 .resume
= kempld_i2c_resume
,
405 module_platform_driver(kempld_i2c_driver
);
407 MODULE_DESCRIPTION("KEM PLD I2C Driver");
408 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
409 MODULE_LICENSE("GPL");
410 MODULE_ALIAS("platform:kempld_i2c");