Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1933175imu; Sat, 10 Nov 2018 04:42:48 -0800 (PST) X-Google-Smtp-Source: AJdET5dN4sHMlCGXpQG+GC26N9/G0W/wOP2TN4UulwWHRlkayI9hd8VW6vPLqUWZHeMpGzZWE5TD X-Received: by 2002:a17:902:8d88:: with SMTP id v8-v6mr12656314plo.21.1541853768446; Sat, 10 Nov 2018 04:42:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541853768; cv=none; d=google.com; s=arc-20160816; b=BnVjztBSnKso4/jA/3aVI9l2lTKqSFH9z65gqnfxnKpXjObu56wNBITzXGL1ilk5Pz Ta9j3IDhXcA/Zd0Hte2P88QXUrNVoro9RPISdoNrouSV4QiqO71olcj2bz9J/LyAo5rg NL7nwsg+UHVVXyqCSIQdHTGlsKg/0Q3Y/2tfXQhH0mlb3aZo1xW8nXDwD5dU78bYZvkJ zuFkqP+x6VIoPrRYZC/wIFKUiDD7W9DkhL5vlbwHElTeZCCSqJsZ0ujG8KKpZk9SvzDH t32zkd5rQyCpkaPZfCrh+gDDG2Y/2mX68nn6BKPHytjk/KQ9nfYlsnvQMlYwJYF7tDFz iYpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=AI2HBz3sENoYIPVL38i1qUTVECzjGaJdNVSe+jw9Cbw=; b=sQJz8UzsQSd9BairFm4vgVzjUJOLHtQnaoFxSR+chDE11BZifI1GAgYtFE9SH9FOxv V4+VZ/gL75/HPHxdA2vkIKlZaZsGfn+QYH9++EOOuwsgEAURMXn632PWlrjY9LEzWVP1 ipX2cNRmbPvOVm2rW1/uoYLuT46CHFbzVkRQrIgAOCjmwmyXmJtEK97pJlAaAvc4sfWP tkv+8i12kJ3yckATjSi6EFzsMfITy2tylTGQ1YnQhbpUpEZU1Z/EkvmkOpbKFqptfE2S 1l20bDVZEh8DS9ZhFlQt+gm96k5xY6q0ZJTrnfv1u0zWbFcez5dmCND7FapTV2SaRwoq ID1g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q3-v6si12939941pli.10.2018.11.10.04.42.32; Sat, 10 Nov 2018 04:42:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729040AbeKJW1G (ORCPT + 99 others); Sat, 10 Nov 2018 17:27:06 -0500 Received: from mail.bootlin.com ([62.4.15.54]:33882 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728974AbeKJW1G (ORCPT ); Sat, 10 Nov 2018 17:27:06 -0500 Received: by mail.bootlin.com (Postfix, from userid 110) id 038CF20731; Sat, 10 Nov 2018 13:42:08 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT shortcircuit=ham autolearn=disabled version=3.4.2 Received: from localhost (unknown [88.191.26.124]) by mail.bootlin.com (Postfix) with ESMTPSA id C1EED206A7; Sat, 10 Nov 2018 13:41:57 +0100 (CET) Date: Sat, 10 Nov 2018 13:41:58 +0100 From: Alexandre Belloni To: zoro Cc: a.zummo@towertech.it, robh+dt@kernel.org, mark.rutland@arm.com, linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] rtc: sd3078: new driver. Message-ID: <20181110124158.GA9674@piout.net> References: <1541839433-28811-1-git-send-email-long17.cool@163.com> <1541839433-28811-2-git-send-email-long17.cool@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1541839433-28811-2-git-send-email-long17.cool@163.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On 10/11/2018 16:43:52+0800, zoro wrote: > diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile > index 290c173..de9b670 100644 > --- a/drivers/rtc/Makefile > +++ b/drivers/rtc/Makefile > @@ -176,3 +176,4 @@ obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o > obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o > obj-$(CONFIG_RTC_DRV_XGENE) += rtc-xgene.o > obj-$(CONFIG_RTC_DRV_ZYNQMP) += rtc-zynqmp.o > +obj-$(CONFIG_RTC_DRV_SD3078) += rtc-sd3078.o This list is sorted alphabetically. > diff --git a/drivers/rtc/rtc-sd3078.c b/drivers/rtc/rtc-sd3078.c > new file mode 100644 > index 0000000..bb24ba1 > --- /dev/null > +++ b/drivers/rtc/rtc-sd3078.c > @@ -0,0 +1,300 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Real Time Clock (RTC) Driver for sd3078 > + * Copyright (C) 2018 Zoro Li > + */ > + > +#include > +#include > +#include > +#include > +#include > + > +#define SD3078_REG_SC 0x00 > +#define SD3078_REG_MN 0x01 > +#define SD3078_REG_HR 0x02 > +#define SD3078_REG_DW 0x03 > +#define SD3078_REG_DM 0x04 > +#define SD3078_REG_MO 0x05 > +#define SD3078_REG_YR 0x06 > + > +#define SD3078_REG_CTRL1 0x0f > +#define SD3078_REG_CTRL2 0x10 > +#define SD3078_REG_CTRL3 0x11 > + > +#define KEY_WRITE1 0x80 > +#define KEY_WRITE2 0x04 > +#define KEY_WRITE3 0x80 > + > +struct sd3078 { > + struct rtc_device *rtc; > +}; > + This structure seems useless as it contains only one member and removing it allows to avoid the allocation in probe. > +struct i2c_driver sd3078_driver; > + This is unnecessary, see below... > + > +static int sd3078_i2c_read_regs(struct i2c_client *client, > + unsigned char reg, unsigned char buf[], unsigned int len) > +{ > + struct i2c_adapter *adap = client->adapter; > + unsigned char reg_buf = reg; > + int ret; > + struct i2c_msg msgs[] = { > + {/* setup read ptr */ > + .addr = client->addr, > + .len = 1, > + .buf = ®_buf > + }, > + {/* read status + date */ > + .addr = client->addr, > + .flags = I2C_M_RD, > + .len = len, > + .buf = buf > + }, > + }; > + > + ret = i2c_transfer(adap, msgs, 2); > + return (ret == 2) ? len : ret; > +} > + > +static int sd3078_i2c_write_reg(struct i2c_client *client, > + unsigned char reg, unsigned char value) > +{ > + unsigned char data[2]; > + int ret; > + > + data[0] = reg; > + data[1] = value; > + ret = i2c_master_send(client, data, 2); > + return ret; > +} > + > +/* > + * In order to prevent arbitrary modification of the time register, > + * when modification of the register, > + * the "write" bit needs to be written in a certain order. > + * 1. set WRITE1 bit > + * 2. set WRITE2 bit > + * 1. set WRITE3 bit > + */ > +static void sd3078_enable_reg_write(struct i2c_client *client) > +{ > + unsigned char reg1, reg2; > + > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL1, ®1, 1); > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL2, ®2, 1); > + > + reg2 |= KEY_WRITE1; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL2, reg2); You should probably use regmap instead of defining your own accessors. This way, you have access to regmap_update_bits which would be shorter. > + > + reg1 |= KEY_WRITE2; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL1, reg1); > + > + reg1 |= KEY_WRITE3; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL1, reg1); > + > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL1, ®1, 1); > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL2, ®2, 1); > +} > + > +/* > + * In order to prevent arbitrary modification of the time register, > + * we should disable the write function. > + * when disable write, > + * the "write" bit needs to be clear in a certain order. > + * 1. clear WRITE2 bit > + * 2. clear WRITE3 bit > + * 3. clear WRITE1 bit > + */ > +static void sd3078_disable_reg_write(struct i2c_client *client) > +{ > + unsigned char reg1, reg2; > + > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL1, ®1, 1); > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL2, ®2, 1); > + > + reg1 &= ~KEY_WRITE2; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL1, reg1); > + > + reg1 &= ~KEY_WRITE3; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL1, reg1); > + > + reg2 &= ~KEY_WRITE1; > + sd3078_i2c_write_reg(client, SD3078_REG_CTRL2, reg2); > + > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL1, ®1, 1); > + sd3078_i2c_read_regs(client, SD3078_REG_CTRL2, ®2, 1); > +} > + > +static int sd3078_get_datetime(struct i2c_client *client, struct rtc_time *tm) > +{ > + unsigned char buf[7] = {0}; > + unsigned char hour; > + > + sd3078_i2c_read_regs(client, SD3078_REG_SC, buf, 7); > + > + tm->tm_sec = bcd2bin(buf[SD3078_REG_SC] & 0x7F); > + tm->tm_min = bcd2bin(buf[SD3078_REG_MN] & 0x7F); > + hour = buf[SD3078_REG_HR]; > + > + if (hour & 0x80) /* 24H PM */ > + tm->tm_hour = bcd2bin(buf[SD3078_REG_HR] & 0x3F); > + else if (hour & 0x20) /* 12H PM */ > + tm->tm_hour = bcd2bin(buf[SD3078_REG_HR] & 0x1F) + 12; > + else /* 12H AM */ > + tm->tm_hour = bcd2bin(buf[SD3078_REG_HR] & 0x1F); > + Is it really useful to support 12H mode? > + tm->tm_mday = bcd2bin(buf[SD3078_REG_DM] & 0x3F); > + tm->tm_wday = buf[SD3078_REG_DW] & 0x07; > + /* rtc mn 1-12 */ > + tm->tm_mon = bcd2bin(buf[SD3078_REG_MO] & 0x1F) - 1; > + tm->tm_year = bcd2bin(buf[SD3078_REG_YR])+100; spaces preferred around that '+' > + > + if (rtc_valid_tm(tm) < 0) This rtc_valid_tm call is useless because this is the first thing the core is doing after calling the read_time callback. > + dev_err(&client->dev, "retrieved date/time is not valid.\n"); > + > + return 0; > +} > + > +static int sd3078_set_datetime(struct i2c_client *client, struct rtc_time *tm) > +{ > + int i, err; > + unsigned char buf[9]; > + > + dev_dbg(&client->dev, > + "%s: secs=%d, mins=%d, hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", > + __func__, > + tm->tm_sec, tm->tm_min, tm->tm_hour, > + tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); > + I don't find that debug to be useful as you already know what the core is passing to the set_time function. > + /* hours, minutes and seconds */ > + buf[SD3078_REG_SC] = bin2bcd(tm->tm_sec); > + buf[SD3078_REG_MN] = bin2bcd(tm->tm_min); > + /* set 24H mode */ > + buf[SD3078_REG_HR] = bin2bcd(tm->tm_hour) | 0x80; > + > + buf[SD3078_REG_DM] = bin2bcd(tm->tm_mday); > + > + /* month, 1 - 12 */ > + buf[SD3078_REG_MO] = bin2bcd(tm->tm_mon) + 1; > + > + /* year and century */ > + buf[SD3078_REG_YR] = bin2bcd(tm->tm_year % 100); please, don't use % 100, instead set the range of the RTC properly in probe. > + buf[SD3078_REG_DW] = tm->tm_wday & 0x07; > + > + sd3078_enable_reg_write(client); > + > + /* write register's data */ > + for (i = 0; i < 7; i++) { > + err = sd3078_i2c_write_reg(client, > + SD3078_REG_SC + i, buf[SD3078_REG_SC + i]); Again, regmap would allow you to avoid this loop > + if (err != 2) > + return -EIO; > + } > + > + sd3078_disable_reg_write(client); I don't find it too useful to enable write protection. It is actually harmful when trying to precisely set the RTC as this adds more latency. But it is up to you to keep it. > + > + return 0; > +} > + > + > +#ifdef CONFIG_RTC_INTF_DEV > +static int sd3078_rtc_ioctl(struct device *dev, > + unsigned int cmd, unsigned long arg) > +{ > + switch (cmd) { > + default: > + return -ENOIOCTLCMD; > + } > +} > +#else > +#define sd3078_rtc_ioctl NULL > +#endif > + This block is totally useless. > +static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm) > +{ > + return sd3078_get_datetime(to_i2c_client(dev), tm); Please remove the useless indirection. > +} > + > +static int sd3078_rtc_set_time(struct device *dev, struct rtc_time *tm) > +{ > + return sd3078_set_datetime(to_i2c_client(dev), tm); Ditto. > +} > + > +static const struct rtc_class_ops sd3078_rtc_ops = { > + .ioctl = sd3078_rtc_ioctl, > + .read_time = sd3078_rtc_read_time, > + .set_time = sd3078_rtc_set_time, > +}; > + > +static int sd3078_probe(struct i2c_client *client, > + const struct i2c_device_id *id) > +{ > + struct sd3078 *sd3078; > + > + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) > + return -ENODEV; > + > + sd3078 = devm_kzalloc(&client->dev, sizeof(*sd3078), GFP_KERNEL); > + if (!sd3078) > + return -ENOMEM; > + > + i2c_set_clientdata(client, sd3078); > + > + sd3078->rtc = devm_rtc_device_register(&client->dev, > + sd3078_driver.driver.name, > + &sd3078_rtc_ops, THIS_MODULE); > + Please use devm_rtc_allocate_device and rtc_register_device. The name is not needed anymore. This also allow you to set the range. Probably something like: rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; rtc->range_max = RTC_TIMESTAMP_END_2099; Maybe you can go up to 2199 if your RTC has a century bit and properly handle the leap day in 2100. > + if (IS_ERR(sd3078->rtc)) > + return PTR_ERR(sd3078->rtc); > + > + return 0; > +} > + > +static int sd3078_remove(struct i2c_client *client) > +{ > + return 0; > +} > + This function is useless. > +static const struct i2c_device_id sd3078_id[] = { > + {"sd3078", 0}, > + { } > +}; > +MODULE_DEVICE_TABLE(i2c, sd3078_id); > + > +#ifdef CONFIG_OF > +static const struct of_device_id rtc_dt_ids[] = { > + { .compatible = "whwave,sd3078" }, > + {}, > +}; This is missing MODULE_DEVICE_TABLE(of, rtc_dt_ids); > +#endif > + > +struct i2c_driver sd3078_driver = { > + .driver = { > + .name = "sd3078", > + .owner = THIS_MODULE, > +#ifdef CONFIG_OF This ifdef is unecessary > + .of_match_table = of_match_ptr(rtc_dt_ids), > +#endif > + }, > + .probe = sd3078_probe, > + .remove = sd3078_remove, > + .id_table = sd3078_id, > +}; > + > +static int __init sd3078_init(void) > +{ > + return i2c_add_driver(&sd3078_driver); > +} > + > +static void __exit sd3078_exit(void) > +{ > + i2c_del_driver(&sd3078_driver); > +} > + > +module_init(sd3078_init); > +module_exit(sd3078_exit); Please use module_i2c_driver instead of open coding. > + > +MODULE_AUTHOR("Zoro Li "); > +MODULE_DESCRIPTION("SD3078 RTC driver"); > +MODULE_LICENSE("GPL"); Your SPDX identifier says GPL v2 while this is GPL v2+. > -- > 1.7.9.5 > -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com