1 // SPDX-License-Identifier: GPL-2.0+
3 * An I2C driver for the Intersil ISL 12026
5 * Copyright (c) 2018 Cavium, Inc.
8 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/nvmem-provider.h>
14 #include <linux/of_device.h>
15 #include <linux/rtc.h>
16 #include <linux/slab.h>
18 /* register offsets */
19 #define ISL12026_REG_PWR 0x14
20 # define ISL12026_REG_PWR_BSW BIT(6)
21 # define ISL12026_REG_PWR_SBIB BIT(7)
22 #define ISL12026_REG_SC 0x30
23 #define ISL12026_REG_HR 0x32
24 # define ISL12026_REG_HR_MIL BIT(7) /* military or 24 hour time */
25 #define ISL12026_REG_SR 0x3f
26 # define ISL12026_REG_SR_RTCF BIT(0)
27 # define ISL12026_REG_SR_WEL BIT(1)
28 # define ISL12026_REG_SR_RWEL BIT(2)
29 # define ISL12026_REG_SR_MBZ BIT(3)
30 # define ISL12026_REG_SR_OSCF BIT(4)
32 /* The EEPROM array responds at i2c address 0x57 */
33 #define ISL12026_EEPROM_ADDR 0x57
35 #define ISL12026_PAGESIZE 16
36 #define ISL12026_NVMEM_WRITE_TIME 20
39 struct rtc_device *rtc;
40 struct i2c_client *nvm_client;
43 static int isl12026_read_reg(struct i2c_client *client, int reg)
49 struct i2c_msg msgs[] = {
63 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
64 if (ret != ARRAY_SIZE(msgs)) {
65 dev_err(&client->dev, "read reg error, ret=%d\n", ret);
66 ret = ret < 0 ? ret : -EIO;
74 static int isl12026_arm_write(struct i2c_client *client)
78 struct i2c_msg msg = {
87 op[1] = ISL12026_REG_SR;
88 op[2] = ISL12026_REG_SR_WEL;
90 ret = i2c_transfer(client->adapter, &msg, 1);
92 dev_err(&client->dev, "write error SR.WEL, ret=%d\n", ret);
93 ret = ret < 0 ? ret : -EIO;
97 /* Set SR.WEL and SR.RWEL */
98 op[2] = ISL12026_REG_SR_WEL | ISL12026_REG_SR_RWEL;
100 ret = i2c_transfer(client->adapter, &msg, 1);
102 dev_err(&client->dev,
103 "write error SR.WEL|SR.RWEL, ret=%d\n", ret);
104 ret = ret < 0 ? ret : -EIO;
113 static int isl12026_disarm_write(struct i2c_client *client)
116 u8 op[3] = {0, ISL12026_REG_SR, 0};
117 struct i2c_msg msg = {
118 .addr = client->addr,
124 ret = i2c_transfer(client->adapter, &msg, 1);
126 dev_err(&client->dev,
127 "write error SR, ret=%d\n", ret);
128 ret = ret < 0 ? ret : -EIO;
136 static int isl12026_write_reg(struct i2c_client *client, int reg, u8 val)
139 u8 op[3] = {0, reg, val};
140 struct i2c_msg msg = {
141 .addr = client->addr,
147 ret = isl12026_arm_write(client);
151 ret = i2c_transfer(client->adapter, &msg, 1);
153 dev_err(&client->dev, "write error CCR, ret=%d\n", ret);
154 ret = ret < 0 ? ret : -EIO;
158 msleep(ISL12026_NVMEM_WRITE_TIME);
160 ret = isl12026_disarm_write(client);
165 static int isl12026_rtc_set_time(struct device *dev, struct rtc_time *tm)
167 struct i2c_client *client = to_i2c_client(dev);
170 struct i2c_msg msg = {
171 .addr = client->addr,
177 ret = isl12026_arm_write(client);
181 /* Set the CCR registers */
183 op[1] = ISL12026_REG_SC;
184 op[2] = bin2bcd(tm->tm_sec); /* SC */
185 op[3] = bin2bcd(tm->tm_min); /* MN */
186 op[4] = bin2bcd(tm->tm_hour) | ISL12026_REG_HR_MIL; /* HR */
187 op[5] = bin2bcd(tm->tm_mday); /* DT */
188 op[6] = bin2bcd(tm->tm_mon + 1); /* MO */
189 op[7] = bin2bcd(tm->tm_year % 100); /* YR */
190 op[8] = bin2bcd(tm->tm_wday & 7); /* DW */
191 op[9] = bin2bcd(tm->tm_year >= 100 ? 20 : 19); /* Y2K */
192 ret = i2c_transfer(client->adapter, &msg, 1);
194 dev_err(&client->dev, "write error CCR, ret=%d\n", ret);
195 ret = ret < 0 ? ret : -EIO;
199 ret = isl12026_disarm_write(client);
204 static int isl12026_rtc_read_time(struct device *dev, struct rtc_time *tm)
206 struct i2c_client *client = to_i2c_client(dev);
211 struct i2c_msg msgs[] = {
213 .addr = client->addr,
218 .addr = client->addr,
225 addr[1] = ISL12026_REG_SR;
229 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
230 if (ret != ARRAY_SIZE(msgs)) {
231 dev_err(&client->dev, "read error, ret=%d\n", ret);
232 ret = ret < 0 ? ret : -EIO;
236 if (sr & ISL12026_REG_SR_RTCF)
237 dev_warn(&client->dev, "Real-Time Clock Failure on read\n");
238 if (sr & ISL12026_REG_SR_OSCF)
239 dev_warn(&client->dev, "Oscillator Failure on read\n");
241 /* Second, CCR regs */
243 addr[1] = ISL12026_REG_SC;
244 msgs[1].len = sizeof(ccr);
247 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
248 if (ret != ARRAY_SIZE(msgs)) {
249 dev_err(&client->dev, "read error, ret=%d\n", ret);
250 ret = ret < 0 ? ret : -EIO;
254 tm->tm_sec = bcd2bin(ccr[0] & 0x7F);
255 tm->tm_min = bcd2bin(ccr[1] & 0x7F);
256 if (ccr[2] & ISL12026_REG_HR_MIL)
257 tm->tm_hour = bcd2bin(ccr[2] & 0x3F);
259 tm->tm_hour = bcd2bin(ccr[2] & 0x1F) +
260 ((ccr[2] & 0x20) ? 12 : 0);
261 tm->tm_mday = bcd2bin(ccr[3] & 0x3F);
262 tm->tm_mon = bcd2bin(ccr[4] & 0x1F) - 1;
263 tm->tm_year = bcd2bin(ccr[5]);
264 if (bcd2bin(ccr[7]) == 20)
266 tm->tm_wday = ccr[6] & 0x07;
273 static const struct rtc_class_ops isl12026_rtc_ops = {
274 .read_time = isl12026_rtc_read_time,
275 .set_time = isl12026_rtc_set_time,
278 static int isl12026_nvm_read(void *p, unsigned int offset,
279 void *val, size_t bytes)
281 struct isl12026 *priv = p;
284 struct i2c_msg msgs[] = {
286 .addr = priv->nvm_client->addr,
291 .addr = priv->nvm_client->addr,
298 * offset and bytes checked and limited by nvmem core, so
299 * proceed without further checks.
301 ret = mutex_lock_interruptible(&priv->rtc->ops_lock);
305 /* 2 bytes of address, most significant first */
306 addr[0] = offset >> 8;
309 ret = i2c_transfer(priv->nvm_client->adapter, msgs, ARRAY_SIZE(msgs));
311 mutex_unlock(&priv->rtc->ops_lock);
313 if (ret != ARRAY_SIZE(msgs)) {
314 dev_err(&priv->nvm_client->dev,
315 "nvmem read error, ret=%d\n", ret);
316 return ret < 0 ? ret : -EIO;
322 static int isl12026_nvm_write(void *p, unsigned int offset,
323 void *val, size_t bytes)
325 struct isl12026 *priv = p;
328 size_t chunk_size, num_written;
329 u8 payload[ISL12026_PAGESIZE + 2]; /* page + 2 address bytes */
330 struct i2c_msg msgs[] = {
332 .addr = priv->nvm_client->addr,
339 * offset and bytes checked and limited by nvmem core, so
340 * proceed without further checks.
342 ret = mutex_lock_interruptible(&priv->rtc->ops_lock);
348 chunk_size = round_down(offset, ISL12026_PAGESIZE) +
349 ISL12026_PAGESIZE - offset;
350 chunk_size = min(bytes, chunk_size);
352 * 2 bytes of address, most significant first, followed
355 memcpy(payload + 2, v + num_written, chunk_size);
356 payload[0] = offset >> 8;
358 msgs[0].len = chunk_size + 2;
359 ret = i2c_transfer(priv->nvm_client->adapter,
360 msgs, ARRAY_SIZE(msgs));
361 if (ret != ARRAY_SIZE(msgs)) {
362 dev_err(&priv->nvm_client->dev,
363 "nvmem write error, ret=%d\n", ret);
364 ret = ret < 0 ? ret : -EIO;
369 offset += chunk_size;
370 num_written += chunk_size;
371 msleep(ISL12026_NVMEM_WRITE_TIME);
374 mutex_unlock(&priv->rtc->ops_lock);
379 static void isl12026_force_power_modes(struct i2c_client *client)
382 int pwr, requested_pwr;
383 u32 bsw_val, sbib_val;
384 bool set_bsw, set_sbib;
387 * If we can read the of_property, set the specified value.
388 * If there is an error reading the of_property (likely
389 * because it does not exist), keep the current value.
391 ret = of_property_read_u32(client->dev.of_node,
392 "isil,pwr-bsw", &bsw_val);
393 set_bsw = (ret == 0);
395 ret = of_property_read_u32(client->dev.of_node,
396 "isil,pwr-sbib", &sbib_val);
397 set_sbib = (ret == 0);
399 /* Check if PWR.BSW and/or PWR.SBIB need specified values */
400 if (!set_bsw && !set_sbib)
403 pwr = isl12026_read_reg(client, ISL12026_REG_PWR);
405 dev_warn(&client->dev, "Error: Failed to read PWR %d\n", pwr);
413 requested_pwr |= ISL12026_REG_PWR_BSW;
415 requested_pwr &= ~ISL12026_REG_PWR_BSW;
416 } /* else keep current BSW */
420 requested_pwr |= ISL12026_REG_PWR_SBIB;
422 requested_pwr &= ~ISL12026_REG_PWR_SBIB;
423 } /* else keep current SBIB */
425 if (pwr >= 0 && pwr != requested_pwr) {
426 dev_dbg(&client->dev, "PWR: %02x\n", pwr);
427 dev_dbg(&client->dev, "Updating PWR to: %02x\n", requested_pwr);
428 isl12026_write_reg(client, ISL12026_REG_PWR, requested_pwr);
432 static int isl12026_probe_new(struct i2c_client *client)
434 struct isl12026 *priv;
436 struct nvmem_config nvm_cfg = {
438 .base_dev = &client->dev,
442 .reg_read = isl12026_nvm_read,
443 .reg_write = isl12026_nvm_write,
446 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
449 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
453 i2c_set_clientdata(client, priv);
455 isl12026_force_power_modes(client);
457 priv->nvm_client = i2c_new_dummy_device(client->adapter, ISL12026_EEPROM_ADDR);
458 if (IS_ERR(priv->nvm_client))
459 return PTR_ERR(priv->nvm_client);
461 priv->rtc = devm_rtc_allocate_device(&client->dev);
462 ret = PTR_ERR_OR_ZERO(priv->rtc);
466 priv->rtc->ops = &isl12026_rtc_ops;
468 ret = devm_rtc_nvmem_register(priv->rtc, &nvm_cfg);
472 return devm_rtc_register_device(priv->rtc);
475 static void isl12026_remove(struct i2c_client *client)
477 struct isl12026 *priv = i2c_get_clientdata(client);
479 i2c_unregister_device(priv->nvm_client);
482 static const struct of_device_id isl12026_dt_match[] = {
483 { .compatible = "isil,isl12026" },
486 MODULE_DEVICE_TABLE(of, isl12026_dt_match);
488 static struct i2c_driver isl12026_driver = {
490 .name = "rtc-isl12026",
491 .of_match_table = isl12026_dt_match,
493 .probe = isl12026_probe_new,
494 .remove = isl12026_remove,
497 module_i2c_driver(isl12026_driver);
499 MODULE_DESCRIPTION("ISL 12026 RTC driver");
500 MODULE_LICENSE("GPL");