1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C bus driver for Conexant Digicolor SoCs
5 * Author: Baruch Siach <baruch@tkos.co.il>
7 * Copyright (C) 2015 Paradox Innovation Ltd.
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
21 #define DEFAULT_FREQ 100000
22 #define TIMEOUT_MS 100
24 #define II_CONTROL 0x0
25 #define II_CONTROL_LOCAL_RESET BIT(0)
27 #define II_CLOCKTIME 0x1
29 #define II_COMMAND 0x2
30 #define II_CMD_START 1
31 #define II_CMD_RESTART 2
32 #define II_CMD_SEND_ACK 3
33 #define II_CMD_GET_ACK 6
34 #define II_CMD_GET_NOACK 7
35 #define II_CMD_STOP 10
36 #define II_COMMAND_GO BIT(7)
37 #define II_COMMAND_COMPLETION_STATUS(r) (((r) >> 5) & 3)
38 #define II_CMD_STATUS_NORMAL 0
39 #define II_CMD_STATUS_ACK_GOOD 1
40 #define II_CMD_STATUS_ACK_BAD 2
41 #define II_CMD_STATUS_ABORT 3
44 #define II_INTFLAG_CLEAR 0x8
45 #define II_INTENABLE 0xa
48 struct i2c_adapter adap
;
52 unsigned int frequency
;
55 unsigned int msgbuf_ptr
;
58 struct completion done
;
72 static void dc_i2c_cmd(struct dc_i2c
*i2c
, u8 cmd
)
74 writeb_relaxed(cmd
| II_COMMAND_GO
, i2c
->regs
+ II_COMMAND
);
77 static u8
dc_i2c_addr_cmd(struct i2c_msg
*msg
)
79 u8 addr
= (msg
->addr
& 0x7f) << 1;
81 if (msg
->flags
& I2C_M_RD
)
87 static void dc_i2c_data(struct dc_i2c
*i2c
, u8 data
)
89 writeb_relaxed(data
, i2c
->regs
+ II_DATA
);
92 static void dc_i2c_write_byte(struct dc_i2c
*i2c
, u8 byte
)
94 dc_i2c_data(i2c
, byte
);
95 dc_i2c_cmd(i2c
, II_CMD_SEND_ACK
);
98 static void dc_i2c_write_buf(struct dc_i2c
*i2c
)
100 dc_i2c_write_byte(i2c
, i2c
->msg
->buf
[i2c
->msgbuf_ptr
++]);
103 static void dc_i2c_next_read(struct dc_i2c
*i2c
)
105 bool last
= (i2c
->msgbuf_ptr
+ 1 == i2c
->msg
->len
);
107 dc_i2c_cmd(i2c
, last
? II_CMD_GET_NOACK
: II_CMD_GET_ACK
);
110 static void dc_i2c_stop(struct dc_i2c
*i2c
)
112 i2c
->state
= STATE_STOP
;
114 dc_i2c_cmd(i2c
, II_CMD_STOP
);
116 complete(&i2c
->done
);
119 static u8
dc_i2c_read_byte(struct dc_i2c
*i2c
)
121 return readb_relaxed(i2c
->regs
+ II_DATA
);
124 static void dc_i2c_read_buf(struct dc_i2c
*i2c
)
126 i2c
->msg
->buf
[i2c
->msgbuf_ptr
++] = dc_i2c_read_byte(i2c
);
127 dc_i2c_next_read(i2c
);
130 static void dc_i2c_set_irq(struct dc_i2c
*i2c
, int enable
)
133 writeb_relaxed(1, i2c
->regs
+ II_INTFLAG_CLEAR
);
134 writeb_relaxed(!!enable
, i2c
->regs
+ II_INTENABLE
);
137 static int dc_i2c_cmd_status(struct dc_i2c
*i2c
)
139 u8 cmd
= readb_relaxed(i2c
->regs
+ II_COMMAND
);
141 return II_COMMAND_COMPLETION_STATUS(cmd
);
144 static void dc_i2c_start_msg(struct dc_i2c
*i2c
, int first
)
146 struct i2c_msg
*msg
= i2c
->msg
;
148 if (!(msg
->flags
& I2C_M_NOSTART
)) {
149 i2c
->state
= STATE_START
;
150 dc_i2c_cmd(i2c
, first
? II_CMD_START
: II_CMD_RESTART
);
151 } else if (msg
->flags
& I2C_M_RD
) {
152 i2c
->state
= STATE_READ
;
153 dc_i2c_next_read(i2c
);
155 i2c
->state
= STATE_WRITE
;
156 dc_i2c_write_buf(i2c
);
160 static irqreturn_t
dc_i2c_irq(int irq
, void *dev_id
)
162 struct dc_i2c
*i2c
= dev_id
;
163 int cmd_status
= dc_i2c_cmd_status(i2c
);
167 writeb_relaxed(1, i2c
->regs
+ II_INTFLAG_CLEAR
);
169 spin_lock_irqsave(&i2c
->lock
, flags
);
171 if (cmd_status
== II_CMD_STATUS_ACK_BAD
172 || cmd_status
== II_CMD_STATUS_ABORT
) {
174 complete(&i2c
->done
);
178 switch (i2c
->state
) {
180 addr_cmd
= dc_i2c_addr_cmd(i2c
->msg
);
181 dc_i2c_write_byte(i2c
, addr_cmd
);
182 i2c
->state
= STATE_ADDR
;
185 if (i2c
->msg
->flags
& I2C_M_RD
) {
186 dc_i2c_next_read(i2c
);
187 i2c
->state
= STATE_READ
;
190 i2c
->state
= STATE_WRITE
;
193 if (i2c
->msgbuf_ptr
< i2c
->msg
->len
)
194 dc_i2c_write_buf(i2c
);
199 if (i2c
->msgbuf_ptr
< i2c
->msg
->len
)
200 dc_i2c_read_buf(i2c
);
205 i2c
->state
= STATE_IDLE
;
206 complete(&i2c
->done
);
211 spin_unlock_irqrestore(&i2c
->lock
, flags
);
215 static int dc_i2c_xfer_msg(struct dc_i2c
*i2c
, struct i2c_msg
*msg
, int first
,
218 unsigned long timeout
= msecs_to_jiffies(TIMEOUT_MS
);
221 spin_lock_irqsave(&i2c
->lock
, flags
);
227 reinit_completion(&i2c
->done
);
228 dc_i2c_set_irq(i2c
, 1);
229 dc_i2c_start_msg(i2c
, first
);
230 spin_unlock_irqrestore(&i2c
->lock
, flags
);
232 timeout
= wait_for_completion_timeout(&i2c
->done
, timeout
);
233 dc_i2c_set_irq(i2c
, 0);
235 i2c
->state
= STATE_IDLE
;
245 static int dc_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
247 struct dc_i2c
*i2c
= adap
->algo_data
;
250 for (i
= 0; i
< num
; i
++) {
251 ret
= dc_i2c_xfer_msg(i2c
, &msgs
[i
], i
== 0, i
== num
- 1);
259 static int dc_i2c_init_hw(struct dc_i2c
*i2c
)
261 unsigned long clk_rate
= clk_get_rate(i2c
->clk
);
262 unsigned int clocktime
;
264 writeb_relaxed(II_CONTROL_LOCAL_RESET
, i2c
->regs
+ II_CONTROL
);
266 writeb_relaxed(0, i2c
->regs
+ II_CONTROL
);
269 clocktime
= DIV_ROUND_UP(clk_rate
, 64 * i2c
->frequency
);
270 if (clocktime
< 1 || clocktime
> 0xff) {
271 dev_err(i2c
->dev
, "can't set bus speed of %u Hz\n",
275 writeb_relaxed(clocktime
- 1, i2c
->regs
+ II_CLOCKTIME
);
280 static u32
dc_i2c_func(struct i2c_adapter
*adap
)
282 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_NOSTART
;
285 static const struct i2c_algorithm dc_i2c_algorithm
= {
286 .master_xfer
= dc_i2c_xfer
,
287 .functionality
= dc_i2c_func
,
290 static int dc_i2c_probe(struct platform_device
*pdev
)
292 struct device_node
*np
= pdev
->dev
.of_node
;
297 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(struct dc_i2c
), GFP_KERNEL
);
301 if (of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
303 i2c
->frequency
= DEFAULT_FREQ
;
305 i2c
->dev
= &pdev
->dev
;
306 platform_set_drvdata(pdev
, i2c
);
308 spin_lock_init(&i2c
->lock
);
309 init_completion(&i2c
->done
);
311 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
312 if (IS_ERR(i2c
->clk
))
313 return PTR_ERR(i2c
->clk
);
315 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
316 i2c
->regs
= devm_ioremap_resource(&pdev
->dev
, r
);
317 if (IS_ERR(i2c
->regs
))
318 return PTR_ERR(i2c
->regs
);
320 irq
= platform_get_irq(pdev
, 0);
324 ret
= devm_request_irq(&pdev
->dev
, irq
, dc_i2c_irq
, 0,
325 dev_name(&pdev
->dev
), i2c
);
329 strlcpy(i2c
->adap
.name
, "Conexant Digicolor I2C adapter",
330 sizeof(i2c
->adap
.name
));
331 i2c
->adap
.owner
= THIS_MODULE
;
332 i2c
->adap
.algo
= &dc_i2c_algorithm
;
333 i2c
->adap
.dev
.parent
= &pdev
->dev
;
334 i2c
->adap
.dev
.of_node
= np
;
335 i2c
->adap
.algo_data
= i2c
;
337 ret
= dc_i2c_init_hw(i2c
);
341 ret
= clk_prepare_enable(i2c
->clk
);
345 ret
= i2c_add_adapter(&i2c
->adap
);
347 clk_disable_unprepare(i2c
->clk
);
354 static int dc_i2c_remove(struct platform_device
*pdev
)
356 struct dc_i2c
*i2c
= platform_get_drvdata(pdev
);
358 i2c_del_adapter(&i2c
->adap
);
359 clk_disable_unprepare(i2c
->clk
);
364 static const struct of_device_id dc_i2c_match
[] = {
365 { .compatible
= "cnxt,cx92755-i2c" },
368 MODULE_DEVICE_TABLE(of
, dc_i2c_match
);
370 static struct platform_driver dc_i2c_driver
= {
371 .probe
= dc_i2c_probe
,
372 .remove
= dc_i2c_remove
,
374 .name
= "digicolor-i2c",
375 .of_match_table
= dc_i2c_match
,
378 module_platform_driver(dc_i2c_driver
);
380 MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
381 MODULE_DESCRIPTION("Conexant Digicolor I2C master driver");
382 MODULE_LICENSE("GPL v2");