BigW Consortium Gitlab

Commit f72d0e55 by David Frey

rtc-pcf85063: simplify code and use appropriate types

parent 90e9b0db
...@@ -47,11 +47,19 @@ ...@@ -47,11 +47,19 @@
#define PCF85063_REG_MO 0x09 #define PCF85063_REG_MO 0x09
#define PCF85063_REG_YR 0x0A #define PCF85063_REG_YR 0x0A
/* Lookup for square-wave clkout frequencies */ /*
#define NUM_FREQUENCIES 8 * The output frequency at index N is generated by writing N the Control_2.COF
static const int clkout_freq_table[NUM_FREQUENCIES][2] = { * register field.
{32768, 0}, {16384, 1}, {8192, 2}, {4096, 3}, */
{2048, 4}, {1024, 5}, {1, 6}, {0, 7} static const u16 clkout_freq_table[] = {
32768,
16384,
8192,
4096,
2048,
1024,
1,
0
}; };
static struct i2c_driver pcf85063_driver; static struct i2c_driver pcf85063_driver;
...@@ -66,46 +74,47 @@ static struct i2c_driver pcf85063_driver; ...@@ -66,46 +74,47 @@ static struct i2c_driver pcf85063_driver;
*/ */
static DEFINE_MUTEX(pcf85063_rtc_mutex); static DEFINE_MUTEX(pcf85063_rtc_mutex);
static int pcf85063_get_clkout_freq(struct device *dev, s32 *freq) static int pcf85063_get_clkout_freq(struct device *dev, u16 *freq)
{ {
struct i2c_client *client = to_i2c_client(dev); struct i2c_client *client = to_i2c_client(dev);
int i; s32 ctrl2_reg_read;
mutex_lock(&pcf85063_rtc_mutex); mutex_lock(&pcf85063_rtc_mutex);
*freq = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL2); ctrl2_reg_read = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL2);
mutex_unlock(&pcf85063_rtc_mutex); mutex_unlock(&pcf85063_rtc_mutex);
if (*freq < 0) { if (ctrl2_reg_read < 0) {
dev_err(&client->dev, "Failed to read PCF85063_REG_CTRL2\n"); dev_err(&client->dev, "Failed to read PCF85063_REG_CTRL2\n");
return -EIO; return -EIO;
} }
*freq &= CLKOUT_FREQ_MASK; *freq = clkout_freq_table[(((u8)ctrl2_reg_read) & CLKOUT_FREQ_MASK)];
for(i = 0; i < NUM_FREQUENCIES && clkout_freq_table[i][1] != *freq; i++)
;
if (i == NUM_FREQUENCIES)
return -EINVAL;
*freq = clkout_freq_table[i][0];
return 0; return 0;
} }
static int pcf85063_set_clkout_freq(struct device *dev, int freq) static int pcf85063_set_clkout_freq(struct device *dev, u16 freq)
{ {
struct i2c_client *client = to_i2c_client(dev); struct i2c_client *client = to_i2c_client(dev);
int i, ret; size_t i;
int ret;
for(i = 0; i < NUM_FREQUENCIES && clkout_freq_table[i][0] != freq; i++) u8 freq_code;
;
if (i == NUM_FREQUENCIES)
return -EINVAL;
freq = clkout_freq_table[i][1]; for (i = 0; i < ARRAY_SIZE(clkout_freq_table); i++) {
freq &= CLKOUT_FREQ_MASK; if (clkout_freq_table[i] == freq)
goto found;
}
return -EINVAL;
found:
freq_code = (i & CLKOUT_FREQ_MASK);
mutex_lock(&pcf85063_rtc_mutex); mutex_lock(&pcf85063_rtc_mutex);
ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL2, (u8) freq); /*
* NOTE: The write below zeros the MI, HMI and TF fields of register
* control_2, but the driver doesn't make use of those fields, so this
* is acceptable.
*/
ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL2, freq_code);
mutex_unlock(&pcf85063_rtc_mutex); mutex_unlock(&pcf85063_rtc_mutex);
if (ret) { if (ret) {
...@@ -268,22 +277,23 @@ static ssize_t pcf85063_sysfs_show_clkout_freq(struct device *dev, ...@@ -268,22 +277,23 @@ static ssize_t pcf85063_sysfs_show_clkout_freq(struct device *dev,
char *buf) char *buf)
{ {
int err; int err;
s32 freq; u16 freq;
err = pcf85063_get_clkout_freq(dev, &freq); err = pcf85063_get_clkout_freq(dev, &freq);
if (err) if (err)
return err; return err;
return sprintf(buf, "%d\n", (int) freq); return sprintf(buf, "%u\n", freq);
} }
static ssize_t pcf85063_sysfs_store_clkout_freq(struct device *dev, static ssize_t pcf85063_sysfs_store_clkout_freq(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
int freq, err; int err;
u16 freq;
if (sscanf(buf, "%i", &freq) != 1) if (kstrtou16(buf, 10, &freq))
return -EINVAL; return -EINVAL;
err = pcf85063_set_clkout_freq(dev, freq); err = pcf85063_set_clkout_freq(dev, freq);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment