summaryrefslogtreecommitdiff
path: root/drivers/i2c/busses/i2c-cgbc.c
blob: eba0b205de1183951b598adaf2f6ac807d09b8ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Congatec Board Controller I2C busses driver
 *
 * Copyright (C) 2024 Bootlin
 * Author: Thomas Richard <thomas.richard@bootlin.com>
 */

#include <linux/i2c.h>
#include <linux/iopoll.h>
#include <linux/mfd/cgbc.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define CGBC_I2C_PRIMARY_BUS_ID	0
#define CGBC_I2C_PM_BUS_ID	4

#define CGBC_I2C_CMD_START	0x40
#define CGBC_I2C_CMD_STAT	0x48
#define CGBC_I2C_CMD_DATA	0x50
#define CGBC_I2C_CMD_SPEED	0x58

#define CGBC_I2C_STAT_IDL	0x00
#define CGBC_I2C_STAT_DAT	0x01
#define CGBC_I2C_STAT_BUSY	0x02

#define CGBC_I2C_START	0x80
#define CGBC_I2C_STOP	0x40

#define CGBC_I2C_LAST_ACK  0x80    /* send ACK on last read byte */

/*
 * Reference code defines 1kHz as min freq and 6.1MHz as max freq.
 * But in practice, the board controller limits the frequency to 1MHz, and the
 * 1kHz is not functional (minimal working freq is 50kHz).
 * So use these values as limits.
 */
#define CGBC_I2C_FREQ_MIN_HZ	50000	/* 50 kHz */
#define CGBC_I2C_FREQ_MAX_HZ	1000000 /* 1 MHz */

#define CGBC_I2C_FREQ_UNIT_1KHZ		0x40
#define CGBC_I2C_FREQ_UNIT_10KHZ	0x80
#define CGBC_I2C_FREQ_UNIT_100KHZ	0xC0

#define CGBC_I2C_FREQ_UNIT_MASK		0xC0
#define CGBC_I2C_FREQ_VALUE_MASK	0x3F

#define CGBC_I2C_READ_MAX_LEN	31
#define CGBC_I2C_WRITE_MAX_LEN	32

#define CGBC_I2C_CMD_HEADER_SIZE	4
#define CGBC_I2C_CMD_SIZE		(CGBC_I2C_CMD_HEADER_SIZE + CGBC_I2C_WRITE_MAX_LEN)

enum cgbc_i2c_state {
	CGBC_I2C_STATE_DONE = 0,
	CGBC_I2C_STATE_INIT,
	CGBC_I2C_STATE_START,
	CGBC_I2C_STATE_READ,
	CGBC_I2C_STATE_WRITE,
	CGBC_I2C_STATE_ERROR,
};

struct i2c_algo_cgbc_data {
	u8		bus_id;
	unsigned long	read_maxtime_us;
};

struct cgbc_i2c_data {
	struct device		*dev;
	struct cgbc_device_data *cgbc;
	struct i2c_adapter      adap;
	struct i2c_msg		*msg;
	int			nmsgs;
	int			pos;
	enum cgbc_i2c_state	state;
};

struct cgbc_i2c_transfer {
	u8 bus_id;
	bool start;
	bool stop;
	bool last_ack;
	u8 read;
	u8 write;
	u8 addr;
	u8 data[CGBC_I2C_WRITE_MAX_LEN];
};

static u8 cgbc_i2c_freq_to_reg(unsigned int bus_frequency)
{
	u8 reg;

	if (bus_frequency <= 10000)
		reg = CGBC_I2C_FREQ_UNIT_1KHZ | (bus_frequency / 1000);
	else if (bus_frequency <= 100000)
		reg = CGBC_I2C_FREQ_UNIT_10KHZ | (bus_frequency / 10000);
	else
		reg = CGBC_I2C_FREQ_UNIT_100KHZ | (bus_frequency / 100000);

	return reg;
}

static unsigned int cgbc_i2c_reg_to_freq(u8 reg)
{
	unsigned int freq = reg & CGBC_I2C_FREQ_VALUE_MASK;
	u8 unit = reg & CGBC_I2C_FREQ_UNIT_MASK;

	if (unit == CGBC_I2C_FREQ_UNIT_100KHZ)
		return freq * 100000;
	else if (unit == CGBC_I2C_FREQ_UNIT_10KHZ)
		return freq * 10000;
	else
		return freq * 1000;
}

static int cgbc_i2c_get_status(struct i2c_adapter *adap)
{
	struct i2c_algo_cgbc_data *algo_data = adap->algo_data;
	struct cgbc_i2c_data *i2c = i2c_get_adapdata(adap);
	struct cgbc_device_data *cgbc = i2c->cgbc;
	u8 cmd = CGBC_I2C_CMD_STAT | algo_data->bus_id;
	u8 status;
	int ret;

	ret = cgbc_command(cgbc, &cmd, sizeof(cmd), NULL, 0, &status);
	if (ret)
		return ret;

	return status;
}

static int cgbc_i2c_set_frequency(struct i2c_adapter *adap,
				  unsigned int bus_frequency)
{
	struct i2c_algo_cgbc_data *algo_data = adap->algo_data;
	struct cgbc_i2c_data *i2c = i2c_get_adapdata(adap);
	struct cgbc_device_data *cgbc = i2c->cgbc;
	u8 cmd[2], data;
	int ret;

	if (bus_frequency > CGBC_I2C_FREQ_MAX_HZ ||
	    bus_frequency < CGBC_I2C_FREQ_MIN_HZ) {
		dev_info(i2c->dev, "invalid frequency %u, using default\n", bus_frequency);
		bus_frequency = I2C_MAX_STANDARD_MODE_FREQ;
	}

	cmd[0] = CGBC_I2C_CMD_SPEED | algo_data->bus_id;
	cmd[1] = cgbc_i2c_freq_to_reg(bus_frequency);

	ret = cgbc_command(cgbc, &cmd, sizeof(cmd), &data, 1, NULL);
	if (ret)
		return dev_err_probe(i2c->dev, ret,
				     "Failed to initialize I2C bus %s",
				     adap->name);

	cmd[1] = 0x00;

	ret = cgbc_command(cgbc, &cmd, sizeof(cmd), &data, 1, NULL);
	if (ret)
		return dev_err_probe(i2c->dev, ret,
				     "Failed to get I2C bus frequency");

	bus_frequency = cgbc_i2c_reg_to_freq(data);

	dev_dbg(i2c->dev, "%s is running at %d Hz\n", adap->name, bus_frequency);

	/*
	 * The read_maxtime_us variable represents the maximum time to wait
	 * for data during a read operation. The maximum amount of data that
	 * can be read by a command is CGBC_I2C_READ_MAX_LEN.
	 * Therefore, calculate the max time to properly size the timeout.
	 */
	algo_data->read_maxtime_us = (BITS_PER_BYTE + 1) * CGBC_I2C_READ_MAX_LEN
		* USEC_PER_SEC / bus_frequency;

	return 0;
}

static unsigned int cgbc_i2c_xfer_to_cmd(struct cgbc_i2c_transfer xfer, u8 *cmd)
{
	int i = 0;

	cmd[i++] = CGBC_I2C_CMD_START | xfer.bus_id;

	cmd[i] = (xfer.start) ? CGBC_I2C_START : 0x00;
	if (xfer.stop)
		cmd[i] |= CGBC_I2C_STOP;
	cmd[i++] |= (xfer.start) ? xfer.write + 1 : xfer.write;

	cmd[i++] = (xfer.last_ack) ? (xfer.read | CGBC_I2C_LAST_ACK) : xfer.read;

	if (xfer.start)
		cmd[i++] = xfer.addr;

	if (xfer.write > 0)
		memcpy(&cmd[i], &xfer.data, xfer.write);

	return i + xfer.write;
}

static int cgbc_i2c_xfer_msg(struct i2c_adapter *adap)
{
	struct i2c_algo_cgbc_data *algo_data = adap->algo_data;
	struct cgbc_i2c_data *i2c = i2c_get_adapdata(adap);
	struct cgbc_device_data *cgbc = i2c->cgbc;
	struct i2c_msg *msg = i2c->msg;
	u8 cmd[CGBC_I2C_CMD_SIZE];
	int ret, max_len, len, i;
	unsigned int cmd_len;
	u8 cmd_data;

	struct cgbc_i2c_transfer xfer = {
		.bus_id = algo_data->bus_id,
		.addr = i2c_8bit_addr_from_msg(msg),
	};

	if (i2c->state == CGBC_I2C_STATE_DONE)
		return 0;

	ret = cgbc_i2c_get_status(adap);

	if (ret == CGBC_I2C_STAT_BUSY)
		return -EBUSY;
	else if (ret < 0)
		goto err;

	if (i2c->state == CGBC_I2C_STATE_INIT ||
	    (i2c->state == CGBC_I2C_STATE_WRITE && msg->flags & I2C_M_RD))
		xfer.start = true;

	i2c->state = (msg->flags & I2C_M_RD) ? CGBC_I2C_STATE_READ : CGBC_I2C_STATE_WRITE;

	max_len = (i2c->state == CGBC_I2C_STATE_READ) ?
		CGBC_I2C_READ_MAX_LEN : CGBC_I2C_WRITE_MAX_LEN;

	if (msg->len - i2c->pos > max_len) {
		len = max_len;
	} else {
		len = msg->len - i2c->pos;

		if (i2c->nmsgs == 1)
			xfer.stop = true;
	}

	if (i2c->state == CGBC_I2C_STATE_WRITE) {
		xfer.write = len;
		xfer.read = 0;

		for (i = 0; i < len; i++)
			xfer.data[i] = msg->buf[i2c->pos + i];

		cmd_len = cgbc_i2c_xfer_to_cmd(xfer, &cmd[0]);

		ret = cgbc_command(cgbc, &cmd, cmd_len, NULL, 0, NULL);
		if (ret)
			goto err;
	} else if (i2c->state == CGBC_I2C_STATE_READ) {
		xfer.write = 0;
		xfer.read = len;

		if (i2c->nmsgs > 1 || msg->len - i2c->pos > max_len)
			xfer.read |= CGBC_I2C_LAST_ACK;

		cmd_len = cgbc_i2c_xfer_to_cmd(xfer, &cmd[0]);
		ret = cgbc_command(cgbc, &cmd, cmd_len, NULL, 0, NULL);
		if (ret)
			goto err;

		ret = read_poll_timeout(cgbc_i2c_get_status, ret,
					ret != CGBC_I2C_STAT_BUSY, 0,
					2 * algo_data->read_maxtime_us, false, adap);
		if (ret < 0)
			goto err;

		cmd_data = CGBC_I2C_CMD_DATA | algo_data->bus_id;
		ret = cgbc_command(cgbc, &cmd_data, sizeof(cmd_data),
				   msg->buf + i2c->pos, len, NULL);
		if (ret)
			goto err;
	}

	if (len == (msg->len - i2c->pos)) {
		i2c->msg++;
		i2c->nmsgs--;
		i2c->pos = 0;
	} else {
		i2c->pos += len;
	}

	if (i2c->nmsgs == 0)
		i2c->state = CGBC_I2C_STATE_DONE;

	return 0;

err:
	i2c->state = CGBC_I2C_STATE_ERROR;
	return ret;
}

static int cgbc_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
			 int num)
{
	struct cgbc_i2c_data *i2c = i2c_get_adapdata(adap);
	unsigned long timeout = jiffies + HZ;
	int ret;

	i2c->state = CGBC_I2C_STATE_INIT;
	i2c->msg = msgs;
	i2c->nmsgs = num;
	i2c->pos = 0;

	while (time_before(jiffies, timeout)) {
		ret = cgbc_i2c_xfer_msg(adap);
		if (i2c->state == CGBC_I2C_STATE_DONE)
			return num;

		if (i2c->state == CGBC_I2C_STATE_ERROR)
			return ret;

		if (ret == 0)
			timeout = jiffies + HZ;
	}

	i2c->state = CGBC_I2C_STATE_ERROR;
	return -ETIMEDOUT;
}

static u32 cgbc_i2c_func(struct i2c_adapter *adap)
{
	return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~(I2C_FUNC_SMBUS_QUICK));
}

static const struct i2c_algorithm cgbc_i2c_algorithm = {
	.master_xfer	= cgbc_i2c_xfer,
	.functionality	= cgbc_i2c_func,
};

static struct i2c_algo_cgbc_data cgbc_i2c_algo_data[] = {
	{ .bus_id = CGBC_I2C_PRIMARY_BUS_ID },
	{ .bus_id = CGBC_I2C_PM_BUS_ID },
};

static const struct i2c_adapter cgbc_i2c_adapter[] = {
	{
		.owner		= THIS_MODULE,
		.name		= "Congatec General Purpose I2C adapter",
		.class		= I2C_CLASS_DEPRECATED,
		.algo		= &cgbc_i2c_algorithm,
		.algo_data	= &cgbc_i2c_algo_data[0],
		.nr		= -1,
	},
	{
		.owner		= THIS_MODULE,
		.name		= "Congatec Power Management I2C adapter",
		.class		= I2C_CLASS_DEPRECATED,
		.algo		= &cgbc_i2c_algorithm,
		.algo_data	= &cgbc_i2c_algo_data[1],
		.nr		= -1,
	},
};

static int cgbc_i2c_probe(struct platform_device *pdev)
{
	struct cgbc_device_data *cgbc = dev_get_drvdata(pdev->dev.parent);
	struct cgbc_i2c_data *i2c;
	int ret;

	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
	if (!i2c)
		return -ENOMEM;

	i2c->cgbc = cgbc;
	i2c->dev = &pdev->dev;
	i2c->adap = cgbc_i2c_adapter[pdev->id];
	i2c->adap.dev.parent = i2c->dev;
	i2c_set_adapdata(&i2c->adap, i2c);
	platform_set_drvdata(pdev, i2c);

	ret = cgbc_i2c_set_frequency(&i2c->adap, I2C_MAX_STANDARD_MODE_FREQ);
	if (ret)
		return ret;

	return i2c_add_numbered_adapter(&i2c->adap);
}

static void cgbc_i2c_remove(struct platform_device *pdev)
{
	struct cgbc_i2c_data *i2c = platform_get_drvdata(pdev);

	i2c_del_adapter(&i2c->adap);
}

static struct platform_driver cgbc_i2c_driver = {
	.driver = {
		.name = "cgbc-i2c",
	},
	.probe		= cgbc_i2c_probe,
	.remove_new	= cgbc_i2c_remove,
};

module_platform_driver(cgbc_i2c_driver);

MODULE_DESCRIPTION("Congatec Board Controller I2C Driver");
MODULE_AUTHOR("Thomas Richard <thomas.richard@bootlin.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:cgbc_i2c");