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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
// SPDX-License-Identifier: GPL-2.0+
/*
* An I2C driver for the Intersil ISL 12026
*
* Copyright (c) 2018 Cavium, Inc.
*/
#include <linux/bcd.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-provider.h>
#include <linux/of.h>
#include <linux/rtc.h>
#include <linux/slab.h>
/* register offsets */
#define ISL12026_REG_PWR 0x14
# define ISL12026_REG_PWR_BSW BIT(6)
# define ISL12026_REG_PWR_SBIB BIT(7)
#define ISL12026_REG_SC 0x30
#define ISL12026_REG_HR 0x32
# define ISL12026_REG_HR_MIL BIT(7) /* military or 24 hour time */
#define ISL12026_REG_SR 0x3f
# define ISL12026_REG_SR_RTCF BIT(0)
# define ISL12026_REG_SR_WEL BIT(1)
# define ISL12026_REG_SR_RWEL BIT(2)
# define ISL12026_REG_SR_MBZ BIT(3)
# define ISL12026_REG_SR_OSCF BIT(4)
/* The EEPROM array responds at i2c address 0x57 */
#define ISL12026_EEPROM_ADDR 0x57
#define ISL12026_PAGESIZE 16
#define ISL12026_NVMEM_WRITE_TIME 20
struct isl12026 {
struct rtc_device *rtc;
struct i2c_client *nvm_client;
};
static int isl12026_read_reg(struct i2c_client *client, int reg)
{
u8 addr[] = {0, reg};
u8 val;
int ret;
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = sizeof(addr),
.buf = addr
}, {
.addr = client->addr,
.flags = I2C_M_RD,
.len = 1,
.buf = &val
}
};
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&client->dev, "read reg error, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
} else {
ret = val;
}
return ret;
}
static int isl12026_arm_write(struct i2c_client *client)
{
int ret;
u8 op[3];
struct i2c_msg msg = {
.addr = client->addr,
.flags = 0,
.len = 1,
.buf = op
};
/* Set SR.WEL */
op[0] = 0;
op[1] = ISL12026_REG_SR;
op[2] = ISL12026_REG_SR_WEL;
msg.len = 3;
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret != 1) {
dev_err(&client->dev, "write error SR.WEL, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
}
/* Set SR.WEL and SR.RWEL */
op[2] = ISL12026_REG_SR_WEL | ISL12026_REG_SR_RWEL;
msg.len = 3;
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret != 1) {
dev_err(&client->dev,
"write error SR.WEL|SR.RWEL, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
} else {
ret = 0;
}
out:
return ret;
}
static int isl12026_disarm_write(struct i2c_client *client)
{
int ret;
u8 op[3] = {0, ISL12026_REG_SR, 0};
struct i2c_msg msg = {
.addr = client->addr,
.flags = 0,
.len = sizeof(op),
.buf = op
};
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret != 1) {
dev_err(&client->dev,
"write error SR, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
} else {
ret = 0;
}
return ret;
}
static int isl12026_write_reg(struct i2c_client *client, int reg, u8 val)
{
int ret;
u8 op[3] = {0, reg, val};
struct i2c_msg msg = {
.addr = client->addr,
.flags = 0,
.len = sizeof(op),
.buf = op
};
ret = isl12026_arm_write(client);
if (ret)
return ret;
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret != 1) {
dev_err(&client->dev, "write error CCR, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
}
msleep(ISL12026_NVMEM_WRITE_TIME);
ret = isl12026_disarm_write(client);
out:
return ret;
}
static int isl12026_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
struct i2c_client *client = to_i2c_client(dev);
int ret;
u8 op[10];
struct i2c_msg msg = {
.addr = client->addr,
.flags = 0,
.len = sizeof(op),
.buf = op
};
ret = isl12026_arm_write(client);
if (ret)
return ret;
/* Set the CCR registers */
op[0] = 0;
op[1] = ISL12026_REG_SC;
op[2] = bin2bcd(tm->tm_sec); /* SC */
op[3] = bin2bcd(tm->tm_min); /* MN */
op[4] = bin2bcd(tm->tm_hour) | ISL12026_REG_HR_MIL; /* HR */
op[5] = bin2bcd(tm->tm_mday); /* DT */
op[6] = bin2bcd(tm->tm_mon + 1); /* MO */
op[7] = bin2bcd(tm->tm_year % 100); /* YR */
op[8] = bin2bcd(tm->tm_wday & 7); /* DW */
op[9] = bin2bcd(tm->tm_year >= 100 ? 20 : 19); /* Y2K */
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret != 1) {
dev_err(&client->dev, "write error CCR, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
}
ret = isl12026_disarm_write(client);
out:
return ret;
}
static int isl12026_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
struct i2c_client *client = to_i2c_client(dev);
u8 ccr[8];
u8 addr[2];
u8 sr;
int ret;
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = sizeof(addr),
.buf = addr
}, {
.addr = client->addr,
.flags = I2C_M_RD,
}
};
/* First, read SR */
addr[0] = 0;
addr[1] = ISL12026_REG_SR;
msgs[1].len = 1;
msgs[1].buf = &sr;
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&client->dev, "read error, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
}
if (sr & ISL12026_REG_SR_RTCF)
dev_warn(&client->dev, "Real-Time Clock Failure on read\n");
if (sr & ISL12026_REG_SR_OSCF)
dev_warn(&client->dev, "Oscillator Failure on read\n");
/* Second, CCR regs */
addr[0] = 0;
addr[1] = ISL12026_REG_SC;
msgs[1].len = sizeof(ccr);
msgs[1].buf = ccr;
ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&client->dev, "read error, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
goto out;
}
tm->tm_sec = bcd2bin(ccr[0] & 0x7F);
tm->tm_min = bcd2bin(ccr[1] & 0x7F);
if (ccr[2] & ISL12026_REG_HR_MIL)
tm->tm_hour = bcd2bin(ccr[2] & 0x3F);
else
tm->tm_hour = bcd2bin(ccr[2] & 0x1F) +
((ccr[2] & 0x20) ? 12 : 0);
tm->tm_mday = bcd2bin(ccr[3] & 0x3F);
tm->tm_mon = bcd2bin(ccr[4] & 0x1F) - 1;
tm->tm_year = bcd2bin(ccr[5]);
if (bcd2bin(ccr[7]) == 20)
tm->tm_year += 100;
tm->tm_wday = ccr[6] & 0x07;
ret = 0;
out:
return ret;
}
static const struct rtc_class_ops isl12026_rtc_ops = {
.read_time = isl12026_rtc_read_time,
.set_time = isl12026_rtc_set_time,
};
static int isl12026_nvm_read(void *p, unsigned int offset,
void *val, size_t bytes)
{
struct isl12026 *priv = p;
int ret;
u8 addr[2];
struct i2c_msg msgs[] = {
{
.addr = priv->nvm_client->addr,
.flags = 0,
.len = sizeof(addr),
.buf = addr
}, {
.addr = priv->nvm_client->addr,
.flags = I2C_M_RD,
.buf = val
}
};
/*
* offset and bytes checked and limited by nvmem core, so
* proceed without further checks.
*/
ret = mutex_lock_interruptible(&priv->rtc->ops_lock);
if (ret)
return ret;
/* 2 bytes of address, most significant first */
addr[0] = offset >> 8;
addr[1] = offset;
msgs[1].len = bytes;
ret = i2c_transfer(priv->nvm_client->adapter, msgs, ARRAY_SIZE(msgs));
mutex_unlock(&priv->rtc->ops_lock);
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&priv->nvm_client->dev,
"nvmem read error, ret=%d\n", ret);
return ret < 0 ? ret : -EIO;
}
return 0;
}
static int isl12026_nvm_write(void *p, unsigned int offset,
void *val, size_t bytes)
{
struct isl12026 *priv = p;
int ret;
u8 *v = val;
size_t chunk_size, num_written;
u8 payload[ISL12026_PAGESIZE + 2]; /* page + 2 address bytes */
struct i2c_msg msgs[] = {
{
.addr = priv->nvm_client->addr,
.flags = 0,
.buf = payload
}
};
/*
* offset and bytes checked and limited by nvmem core, so
* proceed without further checks.
*/
ret = mutex_lock_interruptible(&priv->rtc->ops_lock);
if (ret)
return ret;
num_written = 0;
while (bytes) {
chunk_size = round_down(offset, ISL12026_PAGESIZE) +
ISL12026_PAGESIZE - offset;
chunk_size = min(bytes, chunk_size);
/*
* 2 bytes of address, most significant first, followed
* by page data bytes
*/
memcpy(payload + 2, v + num_written, chunk_size);
payload[0] = offset >> 8;
payload[1] = offset;
msgs[0].len = chunk_size + 2;
ret = i2c_transfer(priv->nvm_client->adapter,
msgs, ARRAY_SIZE(msgs));
if (ret != ARRAY_SIZE(msgs)) {
dev_err(&priv->nvm_client->dev,
"nvmem write error, ret=%d\n", ret);
ret = ret < 0 ? ret : -EIO;
break;
}
ret = 0;
bytes -= chunk_size;
offset += chunk_size;
num_written += chunk_size;
msleep(ISL12026_NVMEM_WRITE_TIME);
}
mutex_unlock(&priv->rtc->ops_lock);
return ret;
}
static void isl12026_force_power_modes(struct i2c_client *client)
{
int ret;
int pwr, requested_pwr;
u32 bsw_val, sbib_val;
bool set_bsw, set_sbib;
/*
* If we can read the of_property, set the specified value.
* If there is an error reading the of_property (likely
* because it does not exist), keep the current value.
*/
ret = of_property_read_u32(client->dev.of_node,
"isil,pwr-bsw", &bsw_val);
set_bsw = (ret == 0);
ret = of_property_read_u32(client->dev.of_node,
"isil,pwr-sbib", &sbib_val);
set_sbib = (ret == 0);
/* Check if PWR.BSW and/or PWR.SBIB need specified values */
if (!set_bsw && !set_sbib)
return;
pwr = isl12026_read_reg(client, ISL12026_REG_PWR);
if (pwr < 0) {
dev_warn(&client->dev, "Error: Failed to read PWR %d\n", pwr);
return;
}
requested_pwr = pwr;
if (set_bsw) {
if (bsw_val)
requested_pwr |= ISL12026_REG_PWR_BSW;
else
requested_pwr &= ~ISL12026_REG_PWR_BSW;
} /* else keep current BSW */
if (set_sbib) {
if (sbib_val)
requested_pwr |= ISL12026_REG_PWR_SBIB;
else
requested_pwr &= ~ISL12026_REG_PWR_SBIB;
} /* else keep current SBIB */
if (pwr >= 0 && pwr != requested_pwr) {
dev_dbg(&client->dev, "PWR: %02x\n", pwr);
dev_dbg(&client->dev, "Updating PWR to: %02x\n", requested_pwr);
isl12026_write_reg(client, ISL12026_REG_PWR, requested_pwr);
}
}
static int isl12026_probe(struct i2c_client *client)
{
struct isl12026 *priv;
int ret;
struct nvmem_config nvm_cfg = {
.name = "isl12026-",
.base_dev = &client->dev,
.stride = 1,
.word_size = 1,
.size = 512,
.reg_read = isl12026_nvm_read,
.reg_write = isl12026_nvm_write,
};
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -ENODEV;
priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
i2c_set_clientdata(client, priv);
isl12026_force_power_modes(client);
priv->nvm_client = i2c_new_dummy_device(client->adapter, ISL12026_EEPROM_ADDR);
if (IS_ERR(priv->nvm_client))
return PTR_ERR(priv->nvm_client);
priv->rtc = devm_rtc_allocate_device(&client->dev);
ret = PTR_ERR_OR_ZERO(priv->rtc);
if (ret)
return ret;
priv->rtc->ops = &isl12026_rtc_ops;
nvm_cfg.priv = priv;
ret = devm_rtc_nvmem_register(priv->rtc, &nvm_cfg);
if (ret)
return ret;
return devm_rtc_register_device(priv->rtc);
}
static void isl12026_remove(struct i2c_client *client)
{
struct isl12026 *priv = i2c_get_clientdata(client);
i2c_unregister_device(priv->nvm_client);
}
static const struct of_device_id isl12026_dt_match[] = {
{ .compatible = "isil,isl12026" },
{ }
};
MODULE_DEVICE_TABLE(of, isl12026_dt_match);
static struct i2c_driver isl12026_driver = {
.driver = {
.name = "rtc-isl12026",
.of_match_table = isl12026_dt_match,
},
.probe = isl12026_probe,
.remove = isl12026_remove,
};
module_i2c_driver(isl12026_driver);
MODULE_DESCRIPTION("ISL 12026 RTC driver");
MODULE_LICENSE("GPL");
|