Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for TI BQ32000 RTC. |
| 3 | * |
| 4 | * Copyright (C) 2009 Semihalf. |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 5 | * Copyright (C) 2014 Pavel Machek <pavel@denx.de> |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 10 | * |
| 11 | * You can get hardware description at |
| 12 | * http://www.ti.com/lit/ds/symlink/bq32000.pdf |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/bcd.h> |
| 21 | |
| 22 | #define BQ32K_SECONDS 0x00 /* Seconds register address */ |
| 23 | #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */ |
| 24 | #define BQ32K_STOP 0x80 /* Oscillator Stop flat */ |
| 25 | |
| 26 | #define BQ32K_MINUTES 0x01 /* Minutes register address */ |
| 27 | #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */ |
| 28 | #define BQ32K_OF 0x80 /* Oscillator Failure flag */ |
| 29 | |
| 30 | #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */ |
| 31 | #define BQ32K_CENT 0x40 /* Century flag */ |
| 32 | #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */ |
| 33 | |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 34 | #define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */ |
| 35 | #define BQ32K_TCH2 0x08 /* Trickle charge enable */ |
| 36 | #define BQ32K_CFG2 0x09 /* Trickle charger control */ |
| 37 | |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 38 | struct bq32k_regs { |
| 39 | uint8_t seconds; |
| 40 | uint8_t minutes; |
| 41 | uint8_t cent_hours; |
| 42 | uint8_t day; |
| 43 | uint8_t date; |
| 44 | uint8_t month; |
| 45 | uint8_t years; |
| 46 | }; |
| 47 | |
| 48 | static struct i2c_driver bq32k_driver; |
| 49 | |
| 50 | static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len) |
| 51 | { |
| 52 | struct i2c_client *client = to_i2c_client(dev); |
| 53 | struct i2c_msg msgs[] = { |
| 54 | { |
| 55 | .addr = client->addr, |
| 56 | .flags = 0, |
| 57 | .len = 1, |
| 58 | .buf = &off, |
| 59 | }, { |
| 60 | .addr = client->addr, |
| 61 | .flags = I2C_M_RD, |
| 62 | .len = len, |
| 63 | .buf = data, |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | if (i2c_transfer(client->adapter, msgs, 2) == 2) |
| 68 | return 0; |
| 69 | |
| 70 | return -EIO; |
| 71 | } |
| 72 | |
| 73 | static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len) |
| 74 | { |
| 75 | struct i2c_client *client = to_i2c_client(dev); |
| 76 | uint8_t buffer[len + 1]; |
| 77 | |
| 78 | buffer[0] = off; |
| 79 | memcpy(&buffer[1], data, len); |
| 80 | |
| 81 | if (i2c_master_send(client, buffer, len + 1) == len + 1) |
| 82 | return 0; |
| 83 | |
| 84 | return -EIO; |
| 85 | } |
| 86 | |
| 87 | static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 88 | { |
| 89 | struct bq32k_regs regs; |
| 90 | int error; |
| 91 | |
| 92 | error = bq32k_read(dev, ®s, 0, sizeof(regs)); |
| 93 | if (error) |
| 94 | return error; |
| 95 | |
Jan Östlund | 421a5ba | 2016-08-11 13:31:44 +0200 | [diff] [blame] | 96 | /* |
| 97 | * In case of oscillator failure, the register contents should be |
| 98 | * considered invalid. The flag is cleared the next time the RTC is set. |
| 99 | */ |
| 100 | if (regs.minutes & BQ32K_OF) |
| 101 | return -EINVAL; |
| 102 | |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 103 | tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK); |
Jan Östlund | 6ed1a51 | 2016-08-11 13:31:43 +0200 | [diff] [blame] | 104 | tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK); |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 105 | tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK); |
| 106 | tm->tm_mday = bcd2bin(regs.date); |
| 107 | tm->tm_wday = bcd2bin(regs.day) - 1; |
| 108 | tm->tm_mon = bcd2bin(regs.month) - 1; |
| 109 | tm->tm_year = bcd2bin(regs.years) + |
| 110 | ((regs.cent_hours & BQ32K_CENT) ? 100 : 0); |
| 111 | |
| 112 | return rtc_valid_tm(tm); |
| 113 | } |
| 114 | |
| 115 | static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 116 | { |
| 117 | struct bq32k_regs regs; |
| 118 | |
| 119 | regs.seconds = bin2bcd(tm->tm_sec); |
| 120 | regs.minutes = bin2bcd(tm->tm_min); |
| 121 | regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN; |
| 122 | regs.day = bin2bcd(tm->tm_wday + 1); |
| 123 | regs.date = bin2bcd(tm->tm_mday); |
| 124 | regs.month = bin2bcd(tm->tm_mon + 1); |
| 125 | |
| 126 | if (tm->tm_year >= 100) { |
| 127 | regs.cent_hours |= BQ32K_CENT; |
| 128 | regs.years = bin2bcd(tm->tm_year - 100); |
| 129 | } else |
| 130 | regs.years = bin2bcd(tm->tm_year); |
| 131 | |
| 132 | return bq32k_write(dev, ®s, 0, sizeof(regs)); |
| 133 | } |
| 134 | |
| 135 | static const struct rtc_class_ops bq32k_rtc_ops = { |
| 136 | .read_time = bq32k_rtc_read_time, |
| 137 | .set_time = bq32k_rtc_set_time, |
| 138 | }; |
| 139 | |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 140 | static int trickle_charger_of_init(struct device *dev, struct device_node *node) |
| 141 | { |
| 142 | unsigned char reg; |
| 143 | int error; |
| 144 | u32 ohms = 0; |
| 145 | |
| 146 | if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms)) |
| 147 | return 0; |
| 148 | |
| 149 | switch (ohms) { |
| 150 | case 180+940: |
| 151 | /* |
| 152 | * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging |
| 153 | * over diode and 940ohm resistor) |
| 154 | */ |
| 155 | |
| 156 | if (of_property_read_bool(node, "trickle-diode-disable")) { |
| 157 | dev_err(dev, "diode and resistor mismatch\n"); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | reg = 0x05; |
| 161 | break; |
| 162 | |
| 163 | case 180+20000: |
| 164 | /* diode disabled */ |
| 165 | |
| 166 | if (!of_property_read_bool(node, "trickle-diode-disable")) { |
| 167 | dev_err(dev, "bq32k: diode and resistor mismatch\n"); |
| 168 | return -EINVAL; |
| 169 | } |
Pavel Machek | 5a6e7599 | 2014-10-29 14:50:42 -0700 | [diff] [blame] | 170 | reg = 0x45; |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | default: |
| 174 | dev_err(dev, "invalid resistor value (%d)\n", ohms); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 178 | error = bq32k_write(dev, ®, BQ32K_CFG2, 1); |
| 179 | if (error) |
| 180 | return error; |
| 181 | |
| 182 | reg = 0x20; |
| 183 | error = bq32k_write(dev, ®, BQ32K_TCH2, 1); |
| 184 | if (error) |
| 185 | return error; |
| 186 | |
| 187 | dev_info(dev, "Enabled trickle RTC battery charge.\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 191 | static int bq32k_probe(struct i2c_client *client, |
| 192 | const struct i2c_device_id *id) |
| 193 | { |
| 194 | struct device *dev = &client->dev; |
| 195 | struct rtc_device *rtc; |
| 196 | uint8_t reg; |
| 197 | int error; |
| 198 | |
| 199 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 200 | return -ENODEV; |
| 201 | |
| 202 | /* Check Oscillator Stop flag */ |
| 203 | error = bq32k_read(dev, ®, BQ32K_SECONDS, 1); |
| 204 | if (!error && (reg & BQ32K_STOP)) { |
| 205 | dev_warn(dev, "Oscillator was halted. Restarting...\n"); |
| 206 | reg &= ~BQ32K_STOP; |
| 207 | error = bq32k_write(dev, ®, BQ32K_SECONDS, 1); |
| 208 | } |
| 209 | if (error) |
| 210 | return error; |
| 211 | |
| 212 | /* Check Oscillator Failure flag */ |
| 213 | error = bq32k_read(dev, ®, BQ32K_MINUTES, 1); |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 214 | if (error) |
| 215 | return error; |
Jan Östlund | 421a5ba | 2016-08-11 13:31:44 +0200 | [diff] [blame] | 216 | if (reg & BQ32K_OF) |
| 217 | dev_warn(dev, "Oscillator Failure. Check RTC battery.\n"); |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 218 | |
Maninder Singh | 617f6f7 | 2015-07-08 12:26:47 +0530 | [diff] [blame] | 219 | if (client->dev.of_node) |
Pavel Machek | 765a98a | 2014-10-13 15:52:50 -0700 | [diff] [blame] | 220 | trickle_charger_of_init(dev, client->dev.of_node); |
| 221 | |
Jingoo Han | 4e44068 | 2013-04-29 16:19:31 -0700 | [diff] [blame] | 222 | rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name, |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 223 | &bq32k_rtc_ops, THIS_MODULE); |
| 224 | if (IS_ERR(rtc)) |
| 225 | return PTR_ERR(rtc); |
| 226 | |
| 227 | i2c_set_clientdata(client, rtc); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 232 | static const struct i2c_device_id bq32k_id[] = { |
| 233 | { "bq32000", 0 }, |
| 234 | { } |
| 235 | }; |
| 236 | MODULE_DEVICE_TABLE(i2c, bq32k_id); |
| 237 | |
| 238 | static struct i2c_driver bq32k_driver = { |
| 239 | .driver = { |
| 240 | .name = "bq32k", |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 241 | }, |
| 242 | .probe = bq32k_probe, |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 243 | .id_table = bq32k_id, |
| 244 | }; |
| 245 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 246 | module_i2c_driver(bq32k_driver); |
Piotr Ziecik | 1ce7c83 | 2009-12-15 16:46:12 -0800 | [diff] [blame] | 247 | |
| 248 | MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>"); |
| 249 | MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver"); |
| 250 | MODULE_LICENSE("GPL"); |