|
23 | 23 | #define NCT3018Y_REG_CTRL 0x0A /* timer control */
|
24 | 24 | #define NCT3018Y_REG_ST 0x0B /* status */
|
25 | 25 | #define NCT3018Y_REG_CLKO 0x0C /* clock out */
|
| 26 | +#define NCT3018Y_REG_PART 0x21 /* part info */ |
26 | 27 |
|
27 | 28 | #define NCT3018Y_BIT_AF BIT(7)
|
28 | 29 | #define NCT3018Y_BIT_ST BIT(7)
|
|
37 | 38 | #define NCT3018Y_REG_BAT_MASK 0x07
|
38 | 39 | #define NCT3018Y_REG_CLKO_F_MASK 0x03 /* frequenc mask */
|
39 | 40 | #define NCT3018Y_REG_CLKO_CKE 0x80 /* clock out enabled */
|
| 41 | +#define NCT3018Y_REG_PART_NCT3018Y 0x02 |
40 | 42 |
|
41 | 43 | struct nct3018y {
|
42 | 44 | struct rtc_device *rtc;
|
43 | 45 | struct i2c_client *client;
|
| 46 | + int part_num; |
44 | 47 | #ifdef CONFIG_COMMON_CLK
|
45 | 48 | struct clk_hw clkout_hw;
|
46 | 49 | #endif
|
@@ -177,8 +180,27 @@ static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
177 | 180 | static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
178 | 181 | {
|
179 | 182 | struct i2c_client *client = to_i2c_client(dev);
|
| 183 | + struct nct3018y *nct3018y = dev_get_drvdata(dev); |
180 | 184 | unsigned char buf[4] = {0};
|
181 |
| - int err; |
| 185 | + int err, flags; |
| 186 | + int restore_flags = 0; |
| 187 | + |
| 188 | + flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL); |
| 189 | + if (flags < 0) { |
| 190 | + dev_dbg(&client->dev, "Failed to read NCT3018Y_REG_CTRL.\n"); |
| 191 | + return flags; |
| 192 | + } |
| 193 | + |
| 194 | + /* Check and set TWO bit */ |
| 195 | + if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y && !(flags & NCT3018Y_BIT_TWO)) { |
| 196 | + restore_flags = 1; |
| 197 | + flags |= NCT3018Y_BIT_TWO; |
| 198 | + err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags); |
| 199 | + if (err < 0) { |
| 200 | + dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n"); |
| 201 | + return err; |
| 202 | + } |
| 203 | + } |
182 | 204 |
|
183 | 205 | buf[0] = bin2bcd(tm->tm_sec);
|
184 | 206 | err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
|
@@ -212,6 +234,18 @@ static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
212 | 234 | return -EIO;
|
213 | 235 | }
|
214 | 236 |
|
| 237 | + /* Restore TWO bit */ |
| 238 | + if (restore_flags) { |
| 239 | + if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y) |
| 240 | + flags &= ~NCT3018Y_BIT_TWO; |
| 241 | + |
| 242 | + err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags); |
| 243 | + if (err < 0) { |
| 244 | + dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n"); |
| 245 | + return err; |
| 246 | + } |
| 247 | + } |
| 248 | + |
215 | 249 | return err;
|
216 | 250 | }
|
217 | 251 |
|
@@ -479,11 +513,17 @@ static int nct3018y_probe(struct i2c_client *client)
|
479 | 513 | dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
|
480 | 514 | }
|
481 | 515 |
|
482 |
| - flags = NCT3018Y_BIT_TWO; |
483 |
| - err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags); |
484 |
| - if (err < 0) { |
485 |
| - dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n"); |
486 |
| - return err; |
| 516 | + nct3018y->part_num = i2c_smbus_read_byte_data(client, NCT3018Y_REG_PART); |
| 517 | + if (nct3018y->part_num < 0) { |
| 518 | + dev_dbg(&client->dev, "Failed to read NCT3018Y_REG_PART.\n"); |
| 519 | + return nct3018y->part_num; |
| 520 | + } else if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y) { |
| 521 | + flags = NCT3018Y_BIT_HF; |
| 522 | + err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags); |
| 523 | + if (err < 0) { |
| 524 | + dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n"); |
| 525 | + return err; |
| 526 | + } |
487 | 527 | }
|
488 | 528 |
|
489 | 529 | flags = 0;
|
|
0 commit comments