1ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport/* 2ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * An rtc/i2c driver for the EM Microelectronic EM3027 3ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * Copyright 2011 CompuLab, Ltd. 4ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * 5ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * Author: Mike Rapoport <mike@compulab.co.il> 6ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * 7ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * Based on rtc-ds1672.c by Alessandro Zummo <a.zummo@towertech.it> 8ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * 9ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * This program is free software; you can redistribute it and/or modify 10ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * it under the terms of the GNU General Public License version 2 as 11ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport * published by the Free Software Foundation. 12ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport */ 13ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 14ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#include <linux/i2c.h> 15ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#include <linux/rtc.h> 16ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#include <linux/bcd.h> 172113852b239ed4a93d04135372162252f9342bb6Paul Gortmaker#include <linux/module.h> 18ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 19ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport/* Registers */ 20ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ON_OFF_CTRL 0x00 21ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_IRQ_CTRL 0x01 22ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_IRQ_FLAGS 0x02 23ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_STATUS 0x03 24ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_RST_CTRL 0x04 25ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 26ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_SEC 0x08 27ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_MIN 0x09 28ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_HOUR 0x0a 29ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_DATE 0x0b 30ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_DAY 0x0c 31ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_MON 0x0d 32ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_WATCH_YEAR 0x0e 33ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 34ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_SEC 0x10 35ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_MIN 0x11 36ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_HOUR 0x12 37ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_DATE 0x13 38ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_DAY 0x14 39ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_MON 0x15 40ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport#define EM3027_REG_ALARM_YEAR 0x16 41ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 42ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic struct i2c_driver em3027_driver; 43ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 44ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic int em3027_get_time(struct device *dev, struct rtc_time *tm) 45ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 46ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct i2c_client *client = to_i2c_client(dev); 47ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 48ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport unsigned char addr = EM3027_REG_WATCH_SEC; 49ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport unsigned char buf[7]; 50ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 51ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct i2c_msg msgs[] = { 52ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport {client->addr, 0, 1, &addr}, /* setup read addr */ 53ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport {client->addr, I2C_M_RD, 7, buf}, /* read time/date */ 54ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport }; 55ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 56ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport /* read time/date registers */ 57ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { 58ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport dev_err(&client->dev, "%s: read error\n", __func__); 59ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return -EIO; 60ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport } 61ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 62ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_sec = bcd2bin(buf[0]); 63ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_min = bcd2bin(buf[1]); 64ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_hour = bcd2bin(buf[2]); 65ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_mday = bcd2bin(buf[3]); 66ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_wday = bcd2bin(buf[4]); 67ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_mon = bcd2bin(buf[5]); 68ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport tm->tm_year = bcd2bin(buf[6]) + 100; 69ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 70ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return 0; 71ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 72ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 73ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic int em3027_set_time(struct device *dev, struct rtc_time *tm) 74ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 75ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct i2c_client *client = to_i2c_client(dev); 76ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport unsigned char buf[8]; 77ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 78ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct i2c_msg msg = { 79ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport client->addr, 0, 8, buf, /* write time/date */ 80ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport }; 81ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 82ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[0] = EM3027_REG_WATCH_SEC; 83ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[1] = bin2bcd(tm->tm_sec); 84ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[2] = bin2bcd(tm->tm_min); 85ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[3] = bin2bcd(tm->tm_hour); 86ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[4] = bin2bcd(tm->tm_mday); 87ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[5] = bin2bcd(tm->tm_wday); 88ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[6] = bin2bcd(tm->tm_mon); 89ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport buf[7] = bin2bcd(tm->tm_year % 100); 90ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 91ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport /* write time/date registers */ 92ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport if ((i2c_transfer(client->adapter, &msg, 1)) != 1) { 93ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport dev_err(&client->dev, "%s: write error\n", __func__); 94ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return -EIO; 95ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport } 96ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 97ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return 0; 98ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 99ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 100ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic const struct rtc_class_ops em3027_rtc_ops = { 101ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .read_time = em3027_get_time, 102ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .set_time = em3027_set_time, 103ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport}; 104ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 105ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic int em3027_probe(struct i2c_client *client, 106ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport const struct i2c_device_id *id) 107ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 108ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct rtc_device *rtc; 109ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 110ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 111ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return -ENODEV; 112ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 113ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport rtc = rtc_device_register(em3027_driver.driver.name, &client->dev, 114ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport &em3027_rtc_ops, THIS_MODULE); 115ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport if (IS_ERR(rtc)) 116ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return PTR_ERR(rtc); 117ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 118ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport i2c_set_clientdata(client, rtc); 119ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 120ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return 0; 121ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 122ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 123ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic int em3027_remove(struct i2c_client *client) 124ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 125ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport struct rtc_device *rtc = i2c_get_clientdata(client); 126ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 127ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport if (rtc) 128ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport rtc_device_unregister(rtc); 129ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 130ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return 0; 131ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 132ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 133ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic struct i2c_device_id em3027_id[] = { 134ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport { "em3027", 0 }, 135ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport { } 136ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport}; 137ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 138ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic struct i2c_driver em3027_driver = { 139ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .driver = { 140ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .name = "rtc-em3027", 141ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport }, 142ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .probe = &em3027_probe, 143ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .remove = &em3027_remove, 144ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport .id_table = em3027_id, 145ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport}; 146ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 147ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic int __init em3027_init(void) 148ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 149ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport return i2c_add_driver(&em3027_driver); 150ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 151ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 152ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportstatic void __exit em3027_exit(void) 153ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport{ 154ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport i2c_del_driver(&em3027_driver); 155ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport} 156ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 157ae3551f9cae5727819d02398b588ac14ef0a9cceMike RapoportMODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); 158ae3551f9cae5727819d02398b588ac14ef0a9cceMike RapoportMODULE_DESCRIPTION("EM Microelectronic EM3027 RTC driver"); 159ae3551f9cae5727819d02398b588ac14ef0a9cceMike RapoportMODULE_LICENSE("GPL"); 160ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoport 161ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportmodule_init(em3027_init); 162ae3551f9cae5727819d02398b588ac14ef0a9cceMike Rapoportmodule_exit(em3027_exit); 163