phy: rockchip-inno-mipi-dphy: fix code style and removed unneeded code
[firefly-linux-kernel-4.4.55.git] / drivers / rtc / rtc-HYM8563.c
index 8288f95d421635420be2e712ad28041f4ffe8133..056bd3e050d549aaf0482e66ae8f1e1d96716f8b 100755 (executable)
 #include <linux/delay.h>
 #include <linux/wakelock.h>
 #include <linux/slab.h>
-#include <mach/gpio.h>
-#include <mach/iomux.h>
 #include "rtc-HYM8563.h"
-#include <mach/board.h>
-
+#include <linux/of_gpio.h>
+#include <linux/irqdomain.h>
 #define RTC_SPEED      200 * 1000
 
 struct hym8563 {
        int irq;
        struct i2c_client *client;
-       struct work_struct work;
        struct mutex mutex;
        struct rtc_device *rtc;
-       int exiting;
        struct rtc_wkalrm alarm;
        struct wake_lock wake_lock;
 };
 static struct i2c_client *gClient = NULL;
 
+static int i2c_master_reg8_send(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
+{
+       struct i2c_adapter *adap=client->adapter;
+       struct i2c_msg msg;
+       int ret;
+       char *tx_buf = (char *)kzalloc(count + 1, GFP_KERNEL);
+       if(!tx_buf)
+               return -ENOMEM;
+       tx_buf[0] = reg;
+       memcpy(tx_buf+1, buf, count); 
+
+       msg.addr = client->addr;
+       msg.flags = client->flags;
+       msg.len = count + 1;
+       msg.buf = (char *)tx_buf;
+       msg.scl_rate = scl_rate;
+
+       ret = i2c_transfer(adap, &msg, 1);
+       kfree(tx_buf);
+       return (ret == 1) ? count : ret;
+
+}
+
+static int i2c_master_reg8_recv(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
+{
+       struct i2c_adapter *adap=client->adapter;
+       struct i2c_msg msgs[2];
+       int ret;
+       char reg_buf = reg;
+       
+       msgs[0].addr = client->addr;
+       msgs[0].flags = client->flags;
+       msgs[0].len = 1;
+       msgs[0].buf = &reg_buf;
+       msgs[0].scl_rate = scl_rate;
+
+       msgs[1].addr = client->addr;
+       msgs[1].flags = client->flags | I2C_M_RD;
+       msgs[1].len = count;
+       msgs[1].buf = (char *)buf;
+       msgs[1].scl_rate = scl_rate;
+
+       ret = i2c_transfer(adap, msgs, 2);
+
+       return (ret == 2)? count : ret;
+}
+
+
+
 static int hym8563_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
 {
        int ret; 
@@ -158,7 +203,7 @@ exit:
 static int hym8563_read_datetime(struct i2c_client *client, struct rtc_time *tm)
 {
        struct hym8563 *hym8563 = i2c_get_clientdata(client);
-       u8 i,regs[HYM8563_RTC_SECTION_LEN] = { 0, };
+       u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
        mutex_lock(&hym8563->mutex);
 //     for (i = 0; i < HYM8563_RTC_SECTION_LEN; i++) {
 //             hym8563_i2c_read_regs(client, RTC_SEC+i, &regs[i], 1);
@@ -182,7 +227,6 @@ static int hym8563_read_datetime(struct i2c_client *client, struct rtc_time *tm)
        else
                tm->tm_year += 2000;
                
-       tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);      
        tm->tm_year -= 1900;                    //inorder to cooperate the systerm time 
        if(tm->tm_year < 0)
                tm->tm_year = 0;        
@@ -204,8 +248,8 @@ static int hym8563_set_time(struct i2c_client *client, struct rtc_time *tm)
 {
        struct hym8563 *hym8563 = i2c_get_clientdata(client);
        u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
-       u8 mon_day,i;
-       u8 ret = 0;
+       u8 mon_day;
+       //u8 ret = 0;
 
        pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
                1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
@@ -493,7 +537,7 @@ static const struct rtc_class_ops hym8563_rtc_ops = {
        .proc           = hym8563_rtc_proc
 };
 
-static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
+static int  hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
        int rc = 0;
        u8 reg = 0;
@@ -508,11 +552,15 @@ static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_d
                .tm_min = 0,
                .tm_sec = 0,
        };      
+
+       struct device_node *np = client->dev.of_node;
+       unsigned long irq_flags;
+       int result;
        
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
                return -ENODEV;
                
-       hym8563 = kzalloc(sizeof(struct hym8563), GFP_KERNEL);
+       hym8563 = devm_kzalloc(&client->dev,sizeof(*hym8563), GFP_KERNEL);
        if (!hym8563) {
                return -ENOMEM;
        }
@@ -520,6 +568,7 @@ static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_d
        gClient = client;       
        hym8563->client = client;
        hym8563->alarm.enabled = 0;
+       client->irq = 0;
        mutex_init(&hym8563->mutex);
        wake_lock_init(&hym8563->wake_lock, WAKE_LOCK_SUSPEND, "rtc_hym8563");
        i2c_set_clientdata(client, hym8563);
@@ -541,24 +590,21 @@ static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_d
                hym8563_set_time(client, &tm);  //initialize the hym8563 
        }       
        
-       if(gpio_request(client->irq, "rtc gpio"))
-       {
-               dev_err(&client->dev, "gpio request fail\n");
-               gpio_free(client->irq);
-               goto exit;
-       }
-       
-       hym8563->irq = gpio_to_irq(client->irq);
-       gpio_pull_updown(client->irq,GPIOPullUp);
-       if (request_threaded_irq(hym8563->irq, NULL, hym8563_wakeup_irq, IRQF_TRIGGER_LOW | IRQF_ONESHOT, client->dev.driver->name, hym8563) < 0)
-       {
-               printk("unable to request rtc irq\n");
-               goto exit;
-       }       
-       enable_irq_wake(hym8563->irq);
-
-       rtc = rtc_device_register(client->name, &client->dev,
-                                 &hym8563_rtc_ops, THIS_MODULE);
+       client->irq = of_get_named_gpio_flags(np, "irq_gpio", 0,(enum of_gpio_flags *)&irq_flags);
+       if(client->irq >= 0)
+        {
+               hym8563->irq = gpio_to_irq(client->irq);
+               result = devm_request_threaded_irq(&client->dev, hym8563->irq, NULL, hym8563_wakeup_irq, irq_flags | IRQF_ONESHOT, client->dev.driver->name,hym8563 );
+               if (result) {
+                       printk(KERN_ERR "%s:fail to request irq = %d, ret = 0x%x\n",__func__, hym8563->irq, result);
+                       goto exit;
+               }
+               enable_irq_wake(hym8563->irq);
+               device_init_wakeup(&client->dev, 1);
+        }
+       rtc = devm_rtc_device_register(&client->dev,
+                       client->name,
+                               &hym8563_rtc_ops, THIS_MODULE);
        if (IS_ERR(rtc)) {
                rc = PTR_ERR(rtc);
                rtc = NULL;
@@ -569,32 +615,17 @@ static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_d
        return 0;
 
 exit:
-       if (rtc)
-               rtc_device_unregister(rtc);
        if (hym8563) {
                wake_lock_destroy(&hym8563->wake_lock);
-               kfree(hym8563);
        }
        return rc;
 }
 
-static int __devexit hym8563_remove(struct i2c_client *client)
+static int  hym8563_remove(struct i2c_client *client)
 {
        struct hym8563 *hym8563 = i2c_get_clientdata(client);
 
-       if (hym8563->irq > 0) {
-               mutex_lock(&hym8563->mutex);
-               hym8563->exiting = 1;
-               mutex_unlock(&hym8563->mutex);
-
-               free_irq(hym8563->irq, hym8563);
-               cancel_work_sync(&hym8563->work);
-       }
-
-       rtc_device_unregister(hym8563->rtc);
        wake_lock_destroy(&hym8563->wake_lock);
-       kfree(hym8563);
-       hym8563 = NULL;
 
        return 0;
 }
@@ -618,18 +649,20 @@ static const struct i2c_device_id hym8563_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, hym8563_id);
 
-static struct i2c_driver hym8563_driver = {
+static struct of_device_id rtc_dt_ids[] = {
+       { .compatible = "rtc,hym8563" },
+       {},
+};
+
+struct i2c_driver hym8563_driver = {
        .driver         = {
                .name   = "rtc_hym8563",
                .owner  = THIS_MODULE,
+               .of_match_table = of_match_ptr(rtc_dt_ids),
        },
        .probe          = hym8563_probe,
-       .remove         = __devexit_p(hym8563_remove),
-#if defined(CONFIG_ARCH_RK3066B) || defined(CONFIG_ARCH_RK3188) || defined(CONFIG_ARCH_RK319X)
+       .remove         = hym8563_remove,
        //.shutdown=hym8563_shutdown,
-#else
-       .shutdown=hym8563_shutdown,
-#endif
        .id_table       = hym8563_id,
 };