| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | #include <linux/bcd.h>
|
| 8 | #include <linux/mfd/rohm-bd71815.h>
|
| 9 | #include <linux/mfd/rohm-bd71828.h>
|
| 10 | #include <linux/mfd/rohm-bd72720.h>
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/of.h>
|
| 13 | #include <linux/platform_device.h>
|
| 14 | #include <linux/regmap.h>
|
| 15 | #include <linux/rtc.h>
|
| 16 |
|
| 17 | |
| 18 | * On BD71828 and BD71815 the ALM0 MASK is 14 bytes after the ALM0
|
| 19 | * block start
|
| 20 |
|
| 21 | #define BD718XX_ALM_EN_OFFSET 14
|
| 22 |
|
| 23 | |
| 24 | * We read regs RTC_SEC => RTC_YEAR
|
| 25 | * this struct is ordered according to chip registers.
|
| 26 | * Keep it u8 only (or packed) to avoid padding issues.
|
| 27 |
|
| 28 | struct bd70528_rtc_day {
|
| 29 | u8 sec;
|
| 30 | u8 min;
|
| 31 | u8 hour;
|
| 32 | } __packed;
|
| 33 |
|
| 34 | struct bd70528_rtc_data {
|
| 35 | struct bd70528_rtc_day time;
|
| 36 | u8 week;
|
| 37 | u8 day;
|
| 38 | u8 month;
|
| 39 | u8 year;
|
| 40 | } __packed;
|
| 41 |
|
| 42 | struct bd71828_rtc_alm {
|
| 43 | struct bd70528_rtc_data alm0;
|
| 44 | struct bd70528_rtc_data alm1;
|
| 45 | u8 alm_mask;
|
| 46 | u8 alm1_mask;
|
| 47 | } __packed;
|
| 48 |
|
| 49 | struct bd70528_rtc {
|
| 50 | struct rohm_regmap_dev *parent;
|
| 51 | struct regmap *regmap;
|
| 52 | struct device *dev;
|
| 53 | u8 reg_time_start;
|
| 54 | u8 bd718xx_alm_block_start;
|
| 55 | };
|
| 56 |
|
| 57 | static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
|
| 58 | {
|
| 59 | d->sec &= ~BD70528_MASK_RTC_SEC;
|
| 60 | d->min &= ~BD70528_MASK_RTC_MINUTE;
|
| 61 | d->hour &= ~BD70528_MASK_RTC_HOUR;
|
| 62 | d->sec |= bin2bcd(t->tm_sec);
|
| 63 | d->min |= bin2bcd(t->tm_min);
|
| 64 | d->hour |= bin2bcd(t->tm_hour);
|
| 65 | }
|
| 66 |
|
| 67 | static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
|
| 68 | {
|
| 69 | r->day &= ~BD70528_MASK_RTC_DAY;
|
| 70 | r->week &= ~BD70528_MASK_RTC_WEEK;
|
| 71 | r->month &= ~BD70528_MASK_RTC_MONTH;
|
| 72 | |
| 73 | * PM and 24H bits are not used by Wake - thus we clear them
|
| 74 | * here and not in tmday2rtc() which is also used by wake.
|
| 75 |
|
| 76 | r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
|
| 77 |
|
| 78 | tmday2rtc(t, &r->time);
|
| 79 | |
| 80 | * We do always set time in 24H mode.
|
| 81 |
|
| 82 | r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
|
| 83 | r->day |= bin2bcd(t->tm_mday);
|
| 84 | r->week |= bin2bcd(t->tm_wday);
|
| 85 | r->month |= bin2bcd(t->tm_mon + 1);
|
| 86 | r->year = bin2bcd(t->tm_year - 100);
|
| 87 | }
|
| 88 |
|
| 89 | static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
|
| 90 | {
|
| 91 | t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
|
| 92 | t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
|
| 93 | t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
|
| 94 | |
| 95 | * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
|
| 96 | * is not BCD value but tells whether it is AM or PM
|
| 97 |
|
| 98 | if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
|
| 99 | t->tm_hour %= 12;
|
| 100 | if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
|
| 101 | t->tm_hour += 12;
|
| 102 | }
|
| 103 | t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
|
| 104 | t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
|
| 105 | t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
|
| 106 | t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
|
| 107 | }
|
| 108 |
|
| 109 | static int bd71828_set_alarm(struct device *dev, struct rtc_wkalrm *a)
|
| 110 | {
|
| 111 | int ret;
|
| 112 | struct bd71828_rtc_alm alm;
|
| 113 | struct bd70528_rtc *r = dev_get_drvdata(dev);
|
| 114 |
|
| 115 | ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
|
| 116 | sizeof(alm));
|
| 117 | if (ret) {
|
| 118 | dev_err(dev, "Failed to read alarm regs\n");
|
| 119 | return ret;
|
| 120 | }
|
| 121 |
|
| 122 | tm2rtc(&a->time, &alm.alm0);
|
| 123 |
|
| 124 | if (!a->enabled)
|
| 125 | alm.alm_mask &= ~BD70528_MASK_ALM_EN;
|
| 126 | else
|
| 127 | alm.alm_mask |= BD70528_MASK_ALM_EN;
|
| 128 |
|
| 129 | ret = regmap_bulk_write(r->regmap, r->bd718xx_alm_block_start, &alm,
|
| 130 | sizeof(alm));
|
| 131 | if (ret)
|
| 132 | dev_err(dev, "Failed to set alarm time\n");
|
| 133 |
|
| 134 | return ret;
|
| 135 |
|
| 136 | }
|
| 137 |
|
| 138 | static int bd71828_read_alarm(struct device *dev, struct rtc_wkalrm *a)
|
| 139 | {
|
| 140 | int ret;
|
| 141 | struct bd71828_rtc_alm alm;
|
| 142 | struct bd70528_rtc *r = dev_get_drvdata(dev);
|
| 143 |
|
| 144 | ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
|
| 145 | sizeof(alm));
|
| 146 | if (ret) {
|
| 147 | dev_err(dev, "Failed to read alarm regs\n");
|
| 148 | return ret;
|
| 149 | }
|
| 150 |
|
| 151 | rtc2tm(&alm.alm0, &a->time);
|
| 152 | a->time.tm_mday = -1;
|
| 153 | a->time.tm_mon = -1;
|
| 154 | a->time.tm_year = -1;
|
| 155 | a->enabled = !!(alm.alm_mask & BD70528_MASK_ALM_EN);
|
| 156 | a->pending = 0;
|
| 157 |
|
| 158 | return 0;
|
| 159 | }
|
| 160 |
|
| 161 | static int bd71828_set_time(struct device *dev, struct rtc_time *t)
|
| 162 | {
|
| 163 | int ret;
|
| 164 | struct bd70528_rtc_data rtc_data;
|
| 165 | struct bd70528_rtc *r = dev_get_drvdata(dev);
|
| 166 |
|
| 167 | ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
|
| 168 | sizeof(rtc_data));
|
| 169 | if (ret) {
|
| 170 | dev_err(dev, "Failed to read RTC time registers\n");
|
| 171 | return ret;
|
| 172 | }
|
| 173 | tm2rtc(t, &rtc_data);
|
| 174 |
|
| 175 | ret = regmap_bulk_write(r->regmap, r->reg_time_start, &rtc_data,
|
| 176 | sizeof(rtc_data));
|
| 177 | if (ret)
|
| 178 | dev_err(dev, "Failed to set RTC time\n");
|
| 179 |
|
| 180 | return ret;
|
| 181 | }
|
| 182 |
|
| 183 | static int bd70528_get_time(struct device *dev, struct rtc_time *t)
|
| 184 | {
|
| 185 | struct bd70528_rtc *r = dev_get_drvdata(dev);
|
| 186 | struct bd70528_rtc_data rtc_data;
|
| 187 | int ret;
|
| 188 |
|
| 189 |
|
| 190 | ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
|
| 191 | sizeof(rtc_data));
|
| 192 | if (ret) {
|
| 193 | dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
|
| 194 | return ret;
|
| 195 | }
|
| 196 |
|
| 197 | rtc2tm(&rtc_data, t);
|
| 198 |
|
| 199 | return 0;
|
| 200 | }
|
| 201 |
|
| 202 | static int bd71828_alm_enable(struct device *dev, unsigned int enabled)
|
| 203 | {
|
| 204 | int ret;
|
| 205 | struct bd70528_rtc *r = dev_get_drvdata(dev);
|
| 206 | unsigned int enableval = BD70528_MASK_ALM_EN;
|
| 207 |
|
| 208 | if (!enabled)
|
| 209 | enableval = 0;
|
| 210 |
|
| 211 | ret = regmap_update_bits(r->regmap, r->bd718xx_alm_block_start +
|
| 212 | BD718XX_ALM_EN_OFFSET, BD70528_MASK_ALM_EN,
|
| 213 | enableval);
|
| 214 | if (ret)
|
| 215 | dev_err(dev, "Failed to change alarm state\n");
|
| 216 |
|
| 217 | return ret;
|
| 218 | }
|
| 219 |
|
| 220 | static const struct rtc_class_ops bd71828_rtc_ops = {
|
| 221 | .read_time = bd70528_get_time,
|
| 222 | .set_time = bd71828_set_time,
|
| 223 | .read_alarm = bd71828_read_alarm,
|
| 224 | .set_alarm = bd71828_set_alarm,
|
| 225 | .alarm_irq_enable = bd71828_alm_enable,
|
| 226 | };
|
| 227 |
|
| 228 | static irqreturn_t alm_hndlr(int irq, void *data)
|
| 229 | {
|
| 230 | struct rtc_device *rtc = data;
|
| 231 |
|
| 232 | rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
|
| 233 | return IRQ_HANDLED;
|
| 234 | }
|
| 235 |
|
| 236 | static int bd70528_probe(struct platform_device *pdev)
|
| 237 | {
|
| 238 | struct bd70528_rtc *bd_rtc;
|
| 239 | const struct rtc_class_ops *rtc_ops;
|
| 240 | int ret;
|
| 241 | struct rtc_device *rtc;
|
| 242 | int irq;
|
| 243 | unsigned int hr;
|
| 244 | u8 hour_reg;
|
| 245 | enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
|
| 246 |
|
| 247 | bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
|
| 248 | if (!bd_rtc)
|
| 249 | return -ENOMEM;
|
| 250 |
|
| 251 | bd_rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
|
| 252 | if (!bd_rtc->regmap) {
|
| 253 | dev_err(&pdev->dev, "No regmap\n");
|
| 254 | return -EINVAL;
|
| 255 | }
|
| 256 |
|
| 257 | bd_rtc->dev = &pdev->dev;
|
| 258 | rtc_ops = &bd71828_rtc_ops;
|
| 259 |
|
| 260 | switch (chip) {
|
| 261 | case ROHM_CHIP_TYPE_BD71815:
|
| 262 | bd_rtc->reg_time_start = BD71815_REG_RTC_START;
|
| 263 |
|
| 264 | |
| 265 | * See also BD718XX_ALM_EN_OFFSET:
|
| 266 | * This works for BD71828, BD71815, and BD72720 as they all
|
| 267 | * have same offset between the ALM0 start and the ALM0_MASK.
|
| 268 | * If new ICs are to be added this requires proper check as
|
| 269 | * the ALM0_MASK is not located at the end of ALM0 block -
|
| 270 | * but after all ALM blocks. If amount of ALMs differ, the
|
| 271 | * offset to enable/disable is likely to be incorrect and
|
| 272 | * enable/disable must be given as own reg address here.
|
| 273 |
|
| 274 | bd_rtc->bd718xx_alm_block_start = BD71815_REG_RTC_ALM_START;
|
| 275 | hour_reg = BD71815_REG_HOUR;
|
| 276 | break;
|
| 277 | case ROHM_CHIP_TYPE_BD71828:
|
| 278 | bd_rtc->reg_time_start = BD71828_REG_RTC_START;
|
| 279 | bd_rtc->bd718xx_alm_block_start = BD71828_REG_RTC_ALM_START;
|
| 280 | hour_reg = BD71828_REG_RTC_HOUR;
|
| 281 | break;
|
| 282 | case ROHM_CHIP_TYPE_BD72720:
|
| 283 | bd_rtc->reg_time_start = BD72720_REG_RTC_START;
|
| 284 | bd_rtc->bd718xx_alm_block_start = BD72720_REG_RTC_ALM_START;
|
| 285 | hour_reg = BD72720_REG_RTC_HOUR;
|
| 286 | break;
|
| 287 | default:
|
| 288 | dev_err(&pdev->dev, "Unknown chip\n");
|
| 289 | return -ENOENT;
|
| 290 | }
|
| 291 |
|
| 292 | irq = platform_get_irq_byname(pdev, "bd70528-rtc-alm-0");
|
| 293 |
|
| 294 | if (irq < 0)
|
| 295 | return irq;
|
| 296 |
|
| 297 | platform_set_drvdata(pdev, bd_rtc);
|
| 298 |
|
| 299 | ret = regmap_read(bd_rtc->regmap, hour_reg, &hr);
|
| 300 |
|
| 301 | if (ret) {
|
| 302 | dev_err(&pdev->dev, "Failed to reag RTC clock\n");
|
| 303 | return ret;
|
| 304 | }
|
| 305 |
|
| 306 | if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
|
| 307 | struct rtc_time t;
|
| 308 |
|
| 309 | ret = rtc_ops->read_time(&pdev->dev, &t);
|
| 310 |
|
| 311 | if (!ret)
|
| 312 | ret = rtc_ops->set_time(&pdev->dev, &t);
|
| 313 |
|
| 314 | if (ret) {
|
| 315 | dev_err(&pdev->dev,
|
| 316 | "Setting 24H clock for RTC failed\n");
|
| 317 | return ret;
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | device_set_wakeup_capable(&pdev->dev, true);
|
| 322 | device_wakeup_enable(&pdev->dev);
|
| 323 |
|
| 324 | rtc = devm_rtc_allocate_device(&pdev->dev);
|
| 325 | if (IS_ERR(rtc)) {
|
| 326 | dev_err(&pdev->dev, "RTC device creation failed\n");
|
| 327 | return PTR_ERR(rtc);
|
| 328 | }
|
| 329 |
|
| 330 | rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
|
| 331 | rtc->range_max = RTC_TIMESTAMP_END_2099;
|
| 332 | rtc->ops = rtc_ops;
|
| 333 |
|
| 334 |
|
| 335 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
|
| 336 | IRQF_ONESHOT, "bd70528-rtc", rtc);
|
| 337 | if (ret)
|
| 338 | return ret;
|
| 339 |
|
| 340 | return devm_rtc_register_device(rtc);
|
| 341 | }
|
| 342 |
|
| 343 | static const struct platform_device_id bd718x7_rtc_id[] = {
|
| 344 | { .name = "bd71828-rtc", .driver_data = ROHM_CHIP_TYPE_BD71828 },
|
| 345 | { .name = "bd71815-rtc", .driver_data = ROHM_CHIP_TYPE_BD71815 },
|
| 346 | { .name = "bd72720-rtc", .driver_data = ROHM_CHIP_TYPE_BD72720 },
|
| 347 | { }
|
| 348 | };
|
| 349 | MODULE_DEVICE_TABLE(platform, bd718x7_rtc_id);
|
| 350 |
|
| 351 | static struct platform_driver bd70528_rtc = {
|
| 352 | .driver = {
|
| 353 | .name = "bd70528-rtc"
|
| 354 | },
|
| 355 | .probe = bd70528_probe,
|
| 356 | .id_table = bd718x7_rtc_id,
|
| 357 | };
|
| 358 |
|
| 359 | module_platform_driver(bd70528_rtc);
|
| 360 |
|
| 361 | MODULE_AUTHOR("Matti Vaittinen <[email protected]>");
|
| 362 | MODULE_DESCRIPTION("ROHM BD71828 and BD71815 PMIC RTC driver");
|
| 363 | MODULE_LICENSE("GPL");
|
| 364 | MODULE_ALIAS("platform:bd70528-rtc");
|