184
184
#define MAX31335_RAM_SIZE 32
185
185
#define MAX31335_TIME_SIZE 0x07
186
186
187
+ /* MAX31331 Register Map */
188
+ #define MAX31331_RTC_CONFIG2 0x04
189
+
187
190
#define clk_hw_to_max31335 (_hw ) container_of(_hw, struct max31335_data, clkout)
188
191
192
+ /* Supported Maxim RTC */
193
+ enum max_rtc_ids {
194
+ ID_MAX31331 ,
195
+ ID_MAX31335 ,
196
+ MAX_RTC_ID_NR
197
+ };
198
+
199
+ struct chip_desc {
200
+ u8 sec_reg ;
201
+ u8 alarm1_sec_reg ;
202
+
203
+ u8 int_en_reg ;
204
+ u8 int_status_reg ;
205
+
206
+ u8 ram_reg ;
207
+ u8 ram_size ;
208
+
209
+ u8 temp_reg ;
210
+
211
+ u8 trickle_reg ;
212
+
213
+ u8 clkout_reg ;
214
+ };
215
+
189
216
struct max31335_data {
217
+ enum max_rtc_ids id ;
190
218
struct regmap * regmap ;
191
219
struct rtc_device * rtc ;
192
220
struct clk_hw clkout ;
221
+ struct clk * clkin ;
222
+ const struct chip_desc * chip ;
223
+ int irq ;
193
224
};
194
225
195
226
static const int max31335_clkout_freq [] = { 1 , 64 , 1024 , 32768 };
196
227
228
+ static const struct chip_desc chip [MAX_RTC_ID_NR ] = {
229
+ [ID_MAX31331 ] = {
230
+ .int_en_reg = 0x01 ,
231
+ .int_status_reg = 0x00 ,
232
+ .sec_reg = 0x08 ,
233
+ .alarm1_sec_reg = 0x0F ,
234
+ .ram_reg = 0x20 ,
235
+ .ram_size = 32 ,
236
+ .trickle_reg = 0x1B ,
237
+ .clkout_reg = 0x04 ,
238
+ },
239
+ [ID_MAX31335 ] = {
240
+ .int_en_reg = 0x01 ,
241
+ .int_status_reg = 0x00 ,
242
+ .sec_reg = 0x0A ,
243
+ .alarm1_sec_reg = 0x11 ,
244
+ .ram_reg = 0x40 ,
245
+ .ram_size = 32 ,
246
+ .temp_reg = 0x35 ,
247
+ .trickle_reg = 0x1D ,
248
+ .clkout_reg = 0x06 ,
249
+ },
250
+ };
251
+
197
252
static const u16 max31335_trickle_resistors [] = {3000 , 6000 , 11000 };
198
253
199
254
static bool max31335_volatile_reg (struct device * dev , unsigned int reg )
200
255
{
256
+ struct max31335_data * max31335 = dev_get_drvdata (dev );
257
+ const struct chip_desc * chip = max31335 -> chip ;
258
+
201
259
/* time keeping registers */
202
- if (reg >= MAX31335_SECONDS &&
203
- reg < MAX31335_SECONDS + MAX31335_TIME_SIZE )
260
+ if (reg >= chip -> sec_reg &&
261
+ reg < chip -> sec_reg + MAX31335_TIME_SIZE )
204
262
return true;
205
263
206
264
/* interrupt status register */
207
- if (reg == MAX31335_STATUS1 )
265
+ if (reg == chip -> int_status_reg )
208
266
return true;
209
267
210
- /* temperature registers */
211
- if (reg == MAX31335_TEMP_DATA_MSB || MAX31335_TEMP_DATA_LSB )
268
+ /* temperature registers if valid*/
269
+ if (chip -> temp_reg &&
270
+ (reg == chip -> temp_reg || reg == chip -> temp_reg + 1 ))
212
271
return true;
213
272
214
273
return false;
@@ -227,7 +286,7 @@ static int max31335_read_time(struct device *dev, struct rtc_time *tm)
227
286
u8 date [7 ];
228
287
int ret ;
229
288
230
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_SECONDS , date ,
289
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
231
290
sizeof (date ));
232
291
if (ret )
233
292
return ret ;
@@ -262,7 +321,7 @@ static int max31335_set_time(struct device *dev, struct rtc_time *tm)
262
321
if (tm -> tm_year >= 200 )
263
322
date [5 ] |= FIELD_PREP (MAX31335_MONTH_CENTURY , 1 );
264
323
265
- return regmap_bulk_write (max31335 -> regmap , MAX31335_SECONDS , date ,
324
+ return regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
266
325
sizeof (date ));
267
326
}
268
327
@@ -273,7 +332,7 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
273
332
struct rtc_time time ;
274
333
u8 regs [6 ];
275
334
276
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_ALM1_SEC , regs ,
335
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg , regs ,
277
336
sizeof (regs ));
278
337
if (ret )
279
338
return ret ;
@@ -292,11 +351,11 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
292
351
if (time .tm_year >= 200 )
293
352
alrm -> time .tm_year += 100 ;
294
353
295
- ret = regmap_read (max31335 -> regmap , MAX31335_INT_EN1 , & ctrl );
354
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_en_reg , & ctrl );
296
355
if (ret )
297
356
return ret ;
298
357
299
- ret = regmap_read (max31335 -> regmap , MAX31335_STATUS1 , & status );
358
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
300
359
if (ret )
301
360
return ret ;
302
361
@@ -320,18 +379,18 @@ static int max31335_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
320
379
regs [4 ] = bin2bcd (alrm -> time .tm_mon + 1 );
321
380
regs [5 ] = bin2bcd (alrm -> time .tm_year % 100 );
322
381
323
- ret = regmap_bulk_write (max31335 -> regmap , MAX31335_ALM1_SEC ,
382
+ ret = regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg ,
324
383
regs , sizeof (regs ));
325
384
if (ret )
326
385
return ret ;
327
386
328
387
reg = FIELD_PREP (MAX31335_INT_EN1_A1IE , alrm -> enabled );
329
- ret = regmap_update_bits (max31335 -> regmap , MAX31335_INT_EN1 ,
388
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_en_reg ,
330
389
MAX31335_INT_EN1_A1IE , reg );
331
390
if (ret )
332
391
return ret ;
333
392
334
- ret = regmap_update_bits (max31335 -> regmap , MAX31335_STATUS1 ,
393
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_status_reg ,
335
394
MAX31335_STATUS1_A1F , 0 );
336
395
337
396
return 0 ;
@@ -341,7 +400,7 @@ static int max31335_alarm_irq_enable(struct device *dev, unsigned int enabled)
341
400
{
342
401
struct max31335_data * max31335 = dev_get_drvdata (dev );
343
402
344
- return regmap_update_bits (max31335 -> regmap , MAX31335_INT_EN1 ,
403
+ return regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_en_reg ,
345
404
MAX31335_INT_EN1_A1IE , enabled );
346
405
}
347
406
@@ -353,12 +412,12 @@ static irqreturn_t max31335_handle_irq(int irq, void *dev_id)
353
412
354
413
mutex_lock (lock );
355
414
356
- ret = regmap_read (max31335 -> regmap , MAX31335_STATUS1 , & status );
415
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
357
416
if (ret )
358
417
goto exit ;
359
418
360
419
if (FIELD_GET (MAX31335_STATUS1_A1F , status )) {
361
- ret = regmap_update_bits (max31335 -> regmap , MAX31335_STATUS1 ,
420
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_status_reg ,
362
421
MAX31335_STATUS1_A1F , 0 );
363
422
if (ret )
364
423
goto exit ;
@@ -414,7 +473,7 @@ static int max31335_trickle_charger_setup(struct device *dev,
414
473
415
474
i = i + trickle_cfg ;
416
475
417
- return regmap_write (max31335 -> regmap , MAX31335_TRICKLE_REG ,
476
+ return regmap_write (max31335 -> regmap , max31335 -> chip -> trickle_reg ,
418
477
FIELD_PREP (MAX31335_TRICKLE_REG_TRICKLE , i ) |
419
478
FIELD_PREP (MAX31335_TRICKLE_REG_EN_TRICKLE ,
420
479
chargeable ));
@@ -428,7 +487,7 @@ static unsigned long max31335_clkout_recalc_rate(struct clk_hw *hw,
428
487
unsigned int reg ;
429
488
int ret ;
430
489
431
- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
490
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
432
491
if (ret )
433
492
return 0 ;
434
493
@@ -454,29 +513,32 @@ static int max31335_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
454
513
struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
455
514
unsigned int freq_mask ;
456
515
int index ;
516
+ int ret ;
457
517
458
518
index = find_closest (rate , max31335_clkout_freq ,
459
519
ARRAY_SIZE (max31335_clkout_freq ));
460
520
freq_mask = __roundup_pow_of_two (ARRAY_SIZE (max31335_clkout_freq )) - 1 ;
461
521
462
- return regmap_update_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
463
- freq_mask , index );
522
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
523
+ freq_mask , index );
524
+
525
+ return ret ;
464
526
}
465
527
466
528
static int max31335_clkout_enable (struct clk_hw * hw )
467
529
{
468
530
struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
469
531
470
- return regmap_set_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
471
- MAX31335_RTC_CONFIG2_ENCLKO );
532
+ return regmap_set_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
533
+ MAX31335_RTC_CONFIG2_ENCLKO );
472
534
}
473
535
474
536
static void max31335_clkout_disable (struct clk_hw * hw )
475
537
{
476
538
struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
477
539
478
- regmap_clear_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
479
- MAX31335_RTC_CONFIG2_ENCLKO );
540
+ regmap_clear_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
541
+ MAX31335_RTC_CONFIG2_ENCLKO );
480
542
}
481
543
482
544
static int max31335_clkout_is_enabled (struct clk_hw * hw )
@@ -485,7 +547,7 @@ static int max31335_clkout_is_enabled(struct clk_hw *hw)
485
547
unsigned int reg ;
486
548
int ret ;
487
549
488
- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
550
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
489
551
if (ret )
490
552
return ret ;
491
553
@@ -510,7 +572,7 @@ static int max31335_nvmem_reg_read(void *priv, unsigned int offset,
510
572
void * val , size_t bytes )
511
573
{
512
574
struct max31335_data * max31335 = priv ;
513
- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
575
+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
514
576
515
577
return regmap_bulk_read (max31335 -> regmap , reg , val , bytes );
516
578
}
@@ -519,7 +581,7 @@ static int max31335_nvmem_reg_write(void *priv, unsigned int offset,
519
581
void * val , size_t bytes )
520
582
{
521
583
struct max31335_data * max31335 = priv ;
522
- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
584
+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
523
585
524
586
return regmap_bulk_write (max31335 -> regmap , reg , val , bytes );
525
587
}
@@ -543,7 +605,7 @@ static int max31335_read_temp(struct device *dev, enum hwmon_sensor_types type,
543
605
if (type != hwmon_temp || attr != hwmon_temp_input )
544
606
return - EOPNOTSUPP ;
545
607
546
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_TEMP_DATA_MSB ,
608
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> temp_reg ,
547
609
reg , 2 );
548
610
if (ret )
549
611
return ret ;
@@ -586,9 +648,10 @@ static int max31335_clkout_register(struct device *dev)
586
648
struct max31335_data * max31335 = dev_get_drvdata (dev );
587
649
int ret ;
588
650
589
- if (!device_property_present (dev , "#clock-cells" ))
590
- return regmap_clear_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
591
- MAX31335_RTC_CONFIG2_ENCLKO );
651
+ if (!device_property_present (dev , "#clock-cells" )) {
652
+ regmap_clear_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
653
+ MAX31335_RTC_CONFIG2_ENCLKO );
654
+ }
592
655
593
656
max31335 -> clkout .init = & max31335_clk_init ;
594
657
@@ -617,6 +680,7 @@ static int max31335_probe(struct i2c_client *client,
617
680
#if IS_REACHABLE (HWMON )
618
681
struct device * hwmon ;
619
682
#endif
683
+ const void * match ;
620
684
int ret ;
621
685
622
686
max31335 = devm_kzalloc (& client -> dev , sizeof (* max31335 ), GFP_KERNEL );
@@ -629,6 +693,16 @@ static int max31335_probe(struct i2c_client *client,
629
693
630
694
i2c_set_clientdata (client , max31335 );
631
695
696
+ match = device_get_match_data (& client -> dev );
697
+ if (match )
698
+ max31335 -> chip = match ;
699
+ else if (id )
700
+ max31335 -> chip = (struct chip_desc * )id -> driver_data ;
701
+ else
702
+ return - ENODEV ;
703
+
704
+ max31335 -> id = max31335 -> chip - chip ;
705
+
632
706
max31335 -> rtc = devm_rtc_allocate_device (& client -> dev );
633
707
if (IS_ERR (max31335 -> rtc ))
634
708
return PTR_ERR (max31335 -> rtc );
@@ -651,6 +725,8 @@ static int max31335_probe(struct i2c_client *client,
651
725
dev_warn (& client -> dev ,
652
726
"unable to request IRQ, alarm max31335 disabled\n" );
653
727
client -> irq = 0 ;
728
+ } else {
729
+ max31335 -> irq = client -> irq ;
654
730
}
655
731
}
656
732
@@ -664,13 +740,15 @@ static int max31335_probe(struct i2c_client *client,
664
740
"cannot register rtc nvmem\n" );
665
741
666
742
#if IS_REACHABLE (HWMON )
667
- hwmon = devm_hwmon_device_register_with_info (& client -> dev , client -> name ,
668
- max31335 ,
669
- & max31335_chip_info ,
670
- NULL );
671
- if (IS_ERR (hwmon ))
672
- return dev_err_probe (& client -> dev , PTR_ERR (hwmon ),
673
- "cannot register hwmon device\n" );
743
+ if (max313xx -> chip -> temp_reg ) {
744
+ hwmon = devm_hwmon_device_register_with_info (& client -> dev , client -> name ,
745
+ max31335 ,
746
+ & max31335_chip_info ,
747
+ NULL );
748
+ if (IS_ERR (hwmon ))
749
+ return dev_err_probe (& client -> dev , PTR_ERR (hwmon ),
750
+ "cannot register hwmon device\n" );
751
+ }
674
752
#endif
675
753
676
754
ret = max31335_trickle_charger_setup (& client -> dev , max31335 );
@@ -681,14 +759,16 @@ static int max31335_probe(struct i2c_client *client,
681
759
}
682
760
683
761
static const struct i2c_device_id max31335_id [] = {
684
- { "max31335" , 0 },
762
+ { "max31331" , (kernel_ulong_t )& chip [ID_MAX31331 ] },
763
+ { "max31335" , (kernel_ulong_t )& chip [ID_MAX31335 ] },
685
764
{ }
686
765
};
687
766
688
767
MODULE_DEVICE_TABLE (i2c , max31335_id );
689
768
690
769
static const struct of_device_id max31335_of_match [] = {
691
- { .compatible = "adi,max31335" },
770
+ { .compatible = "adi,max31331" , .data = & chip [ID_MAX31331 ] },
771
+ { .compatible = "adi,max31335" , .data = & chip [ID_MAX31335 ] },
692
772
{ }
693
773
};
694
774
@@ -705,5 +785,6 @@ static struct i2c_driver max31335_driver = {
705
785
module_i2c_driver (max31335_driver );
706
786
707
787
MODULE_AUTHOR ("Antoniu Miclaus <antoniu.miclaus@analog.com>" );
788
+ MODULE_AUTHOR ("Saket Kumar Purwar <Saket.Kumarpurwar@analog.com>" );
708
789
MODULE_DESCRIPTION ("MAX31335 RTC driver" );
709
790
MODULE_LICENSE ("GPL" );
0 commit comments