| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for Epson's RTC module RX-8025 SA/NB |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 13 | 14 | * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com> |
|---|
| 14 | 15 | * Converted to new style by Wolfgang Grandegger <wg@grandegger.com> |
|---|
| 15 | 16 | * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com> |
|---|
| 16 | | - * |
|---|
| 17 | | - * This program is free software; you can redistribute it and/or |
|---|
| 18 | | - * modify it under the terms of the GNU General Public License |
|---|
| 19 | | - * version 2 as published by the Free Software Foundation. |
|---|
| 20 | 17 | */ |
|---|
| 21 | 18 | #include <linux/bcd.h> |
|---|
| 22 | 19 | #include <linux/bitops.h> |
|---|
| .. | .. |
|---|
| 70 | 67 | MODULE_DEVICE_TABLE(i2c, rx8025_id); |
|---|
| 71 | 68 | |
|---|
| 72 | 69 | struct rx8025_data { |
|---|
| 73 | | - struct i2c_client *client; |
|---|
| 74 | 70 | struct rtc_device *rtc; |
|---|
| 75 | 71 | u8 ctrl1; |
|---|
| 76 | 72 | }; |
|---|
| .. | .. |
|---|
| 106 | 102 | |
|---|
| 107 | 103 | static int rx8025_check_validity(struct device *dev) |
|---|
| 108 | 104 | { |
|---|
| 109 | | - struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
|---|
| 105 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 110 | 106 | int ctrl2; |
|---|
| 111 | 107 | |
|---|
| 112 | | - ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2); |
|---|
| 108 | + ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2); |
|---|
| 113 | 109 | if (ctrl2 < 0) |
|---|
| 114 | 110 | return ctrl2; |
|---|
| 115 | 111 | |
|---|
| .. | .. |
|---|
| 181 | 177 | |
|---|
| 182 | 178 | static int rx8025_get_time(struct device *dev, struct rtc_time *dt) |
|---|
| 183 | 179 | { |
|---|
| 180 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 184 | 181 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
|---|
| 185 | 182 | u8 date[7]; |
|---|
| 186 | 183 | int err; |
|---|
| .. | .. |
|---|
| 189 | 186 | if (err) |
|---|
| 190 | 187 | return err; |
|---|
| 191 | 188 | |
|---|
| 192 | | - err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
|---|
| 189 | + err = rx8025_read_regs(client, RX8025_REG_SEC, 7, date); |
|---|
| 193 | 190 | if (err) |
|---|
| 194 | 191 | return err; |
|---|
| 195 | 192 | |
|---|
| 196 | | - dev_dbg(dev, "%s: read 0x%02x 0x%02x " |
|---|
| 197 | | - "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__, |
|---|
| 198 | | - date[0], date[1], date[2], date[3], date[4], |
|---|
| 199 | | - date[5], date[6]); |
|---|
| 193 | + dev_dbg(dev, "%s: read %7ph\n", __func__, date); |
|---|
| 200 | 194 | |
|---|
| 201 | 195 | dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f); |
|---|
| 202 | 196 | dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f); |
|---|
| .. | .. |
|---|
| 210 | 204 | dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1; |
|---|
| 211 | 205 | dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100; |
|---|
| 212 | 206 | |
|---|
| 213 | | - dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__, |
|---|
| 214 | | - dt->tm_sec, dt->tm_min, dt->tm_hour, |
|---|
| 215 | | - dt->tm_mday, dt->tm_mon, dt->tm_year); |
|---|
| 207 | + dev_dbg(dev, "%s: date %ptRr\n", __func__, dt); |
|---|
| 216 | 208 | |
|---|
| 217 | 209 | return 0; |
|---|
| 218 | 210 | } |
|---|
| 219 | 211 | |
|---|
| 220 | 212 | static int rx8025_set_time(struct device *dev, struct rtc_time *dt) |
|---|
| 221 | 213 | { |
|---|
| 214 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 222 | 215 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
|---|
| 223 | 216 | u8 date[7]; |
|---|
| 224 | 217 | int ret; |
|---|
| .. | .. |
|---|
| 243 | 236 | date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1); |
|---|
| 244 | 237 | date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100); |
|---|
| 245 | 238 | |
|---|
| 246 | | - dev_dbg(dev, |
|---|
| 247 | | - "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", |
|---|
| 248 | | - __func__, |
|---|
| 249 | | - date[0], date[1], date[2], date[3], date[4], date[5], date[6]); |
|---|
| 239 | + dev_dbg(dev, "%s: write %7ph\n", __func__, date); |
|---|
| 250 | 240 | |
|---|
| 251 | | - ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date); |
|---|
| 241 | + ret = rx8025_write_regs(client, RX8025_REG_SEC, 7, date); |
|---|
| 252 | 242 | if (ret < 0) |
|---|
| 253 | 243 | return ret; |
|---|
| 254 | 244 | |
|---|
| 255 | | - return rx8025_reset_validity(rx8025->client); |
|---|
| 245 | + return rx8025_reset_validity(client); |
|---|
| 256 | 246 | } |
|---|
| 257 | 247 | |
|---|
| 258 | 248 | static int rx8025_init_client(struct i2c_client *client) |
|---|
| .. | .. |
|---|
| 262 | 252 | int need_clear = 0; |
|---|
| 263 | 253 | int err; |
|---|
| 264 | 254 | |
|---|
| 265 | | - err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl); |
|---|
| 255 | + err = rx8025_read_regs(client, RX8025_REG_CTRL1, 2, ctrl); |
|---|
| 266 | 256 | if (err) |
|---|
| 267 | 257 | goto out; |
|---|
| 268 | 258 | |
|---|
| .. | .. |
|---|
| 291 | 281 | /* Alarm support */ |
|---|
| 292 | 282 | static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
|---|
| 293 | 283 | { |
|---|
| 284 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 294 | 285 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
|---|
| 295 | | - struct i2c_client *client = rx8025->client; |
|---|
| 296 | 286 | u8 ald[2]; |
|---|
| 297 | 287 | int ctrl2, err; |
|---|
| 298 | 288 | |
|---|
| .. | .. |
|---|
| 319 | 309 | t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12 |
|---|
| 320 | 310 | + (ald[1] & 0x20 ? 12 : 0); |
|---|
| 321 | 311 | |
|---|
| 322 | | - dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n", |
|---|
| 323 | | - __func__, |
|---|
| 324 | | - t->time.tm_sec, t->time.tm_min, t->time.tm_hour, |
|---|
| 325 | | - t->time.tm_mday, t->time.tm_mon, t->time.tm_year); |
|---|
| 312 | + dev_dbg(dev, "%s: date: %ptRr\n", __func__, &t->time); |
|---|
| 326 | 313 | t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE); |
|---|
| 327 | 314 | t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled; |
|---|
| 328 | 315 | |
|---|
| .. | .. |
|---|
| 361 | 348 | |
|---|
| 362 | 349 | if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) { |
|---|
| 363 | 350 | rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE; |
|---|
| 364 | | - err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
|---|
| 351 | + err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
|---|
| 365 | 352 | rx8025->ctrl1); |
|---|
| 366 | 353 | if (err) |
|---|
| 367 | 354 | return err; |
|---|
| 368 | 355 | } |
|---|
| 369 | | - err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald); |
|---|
| 356 | + err = rx8025_write_regs(client, RX8025_REG_ALDMIN, 2, ald); |
|---|
| 370 | 357 | if (err) |
|---|
| 371 | 358 | return err; |
|---|
| 372 | 359 | |
|---|
| 373 | 360 | if (t->enabled) { |
|---|
| 374 | 361 | rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE; |
|---|
| 375 | | - err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
|---|
| 362 | + err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
|---|
| 376 | 363 | rx8025->ctrl1); |
|---|
| 377 | 364 | if (err) |
|---|
| 378 | 365 | return err; |
|---|
| .. | .. |
|---|
| 383 | 370 | |
|---|
| 384 | 371 | static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled) |
|---|
| 385 | 372 | { |
|---|
| 373 | + struct i2c_client *client = to_i2c_client(dev); |
|---|
| 386 | 374 | struct rx8025_data *rx8025 = dev_get_drvdata(dev); |
|---|
| 387 | 375 | u8 ctrl1; |
|---|
| 388 | 376 | int err; |
|---|
| .. | .. |
|---|
| 395 | 383 | |
|---|
| 396 | 384 | if (ctrl1 != rx8025->ctrl1) { |
|---|
| 397 | 385 | rx8025->ctrl1 = ctrl1; |
|---|
| 398 | | - err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1, |
|---|
| 386 | + err = rx8025_write_reg(client, RX8025_REG_CTRL1, |
|---|
| 399 | 387 | rx8025->ctrl1); |
|---|
| 400 | 388 | if (err) |
|---|
| 401 | 389 | return err; |
|---|
| .. | .. |
|---|
| 515 | 503 | static int rx8025_probe(struct i2c_client *client, |
|---|
| 516 | 504 | const struct i2c_device_id *id) |
|---|
| 517 | 505 | { |
|---|
| 518 | | - struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
|---|
| 506 | + struct i2c_adapter *adapter = client->adapter; |
|---|
| 519 | 507 | struct rx8025_data *rx8025; |
|---|
| 520 | 508 | int err = 0; |
|---|
| 521 | 509 | |
|---|
| .. | .. |
|---|
| 530 | 518 | if (!rx8025) |
|---|
| 531 | 519 | return -ENOMEM; |
|---|
| 532 | 520 | |
|---|
| 533 | | - rx8025->client = client; |
|---|
| 534 | 521 | i2c_set_clientdata(client, rx8025); |
|---|
| 535 | 522 | |
|---|
| 536 | 523 | err = rx8025_init_client(client); |
|---|