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
+ enum max_rtc_ids id ;
216
+ };
217
+
189
218
struct max31335_data {
190
219
struct regmap * regmap ;
191
220
struct rtc_device * rtc ;
192
221
struct clk_hw clkout ;
222
+ struct clk * clkin ;
223
+ const struct chip_desc * chip ;
224
+ int irq ;
193
225
};
194
226
195
227
static const int max31335_clkout_freq [] = { 1 , 64 , 1024 , 32768 };
196
228
229
+ static const struct chip_desc chip [MAX_RTC_ID_NR ] = {
230
+ [ID_MAX31331 ] = {
231
+ .id = ID_MAX31331 ,
232
+ .int_en_reg = 0x01 ,
233
+ .int_status_reg = 0x00 ,
234
+ .sec_reg = 0x08 ,
235
+ .alarm1_sec_reg = 0x0F ,
236
+ .ram_reg = 0x20 ,
237
+ .ram_size = 32 ,
238
+ .trickle_reg = 0x1B ,
239
+ .clkout_reg = 0x04 ,
240
+ },
241
+ [ID_MAX31335 ] = {
242
+ .id = ID_MAX31335 ,
243
+ .int_en_reg = 0x01 ,
244
+ .int_status_reg = 0x00 ,
245
+ .sec_reg = 0x0A ,
246
+ .alarm1_sec_reg = 0x11 ,
247
+ .ram_reg = 0x40 ,
248
+ .ram_size = 32 ,
249
+ .temp_reg = 0x35 ,
250
+ .trickle_reg = 0x1D ,
251
+ .clkout_reg = 0x06 ,
252
+ },
253
+ };
254
+
197
255
static const u16 max31335_trickle_resistors [] = {3000 , 6000 , 11000 };
198
256
199
257
static bool max31335_volatile_reg (struct device * dev , unsigned int reg )
200
258
{
259
+ struct max31335_data * max31335 = dev_get_drvdata (dev );
260
+ const struct chip_desc * chip = max31335 -> chip ;
261
+
201
262
/* time keeping registers */
202
- if (reg >= MAX31335_SECONDS &&
203
- reg < MAX31335_SECONDS + MAX31335_TIME_SIZE )
263
+ if (reg >= chip -> sec_reg && reg < chip -> sec_reg + MAX31335_TIME_SIZE )
204
264
return true;
205
265
206
266
/* interrupt status register */
207
- if (reg == MAX31335_STATUS1 )
267
+ if (reg == chip -> int_status_reg )
208
268
return true;
209
269
210
- /* temperature registers */
211
- if (reg == MAX31335_TEMP_DATA_MSB || reg == MAX31335_TEMP_DATA_LSB )
270
+ /* temperature registers if valid */
271
+ if (chip -> temp_reg && ( reg == chip -> temp_reg || reg == chip -> temp_reg + 1 ) )
212
272
return true;
213
273
214
274
return false;
@@ -227,7 +287,7 @@ static int max31335_read_time(struct device *dev, struct rtc_time *tm)
227
287
u8 date [7 ];
228
288
int ret ;
229
289
230
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_SECONDS , date ,
290
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
231
291
sizeof (date ));
232
292
if (ret )
233
293
return ret ;
@@ -262,7 +322,7 @@ static int max31335_set_time(struct device *dev, struct rtc_time *tm)
262
322
if (tm -> tm_year >= 200 )
263
323
date [5 ] |= FIELD_PREP (MAX31335_MONTH_CENTURY , 1 );
264
324
265
- return regmap_bulk_write (max31335 -> regmap , MAX31335_SECONDS , date ,
325
+ return regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
266
326
sizeof (date ));
267
327
}
268
328
@@ -273,7 +333,7 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
273
333
struct rtc_time time ;
274
334
u8 regs [6 ];
275
335
276
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_ALM1_SEC , regs ,
336
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg , regs ,
277
337
sizeof (regs ));
278
338
if (ret )
279
339
return ret ;
@@ -292,11 +352,11 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
292
352
if (time .tm_year >= 200 )
293
353
alrm -> time .tm_year += 100 ;
294
354
295
- ret = regmap_read (max31335 -> regmap , MAX31335_INT_EN1 , & ctrl );
355
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_en_reg , & ctrl );
296
356
if (ret )
297
357
return ret ;
298
358
299
- ret = regmap_read (max31335 -> regmap , MAX31335_STATUS1 , & status );
359
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
300
360
if (ret )
301
361
return ret ;
302
362
@@ -320,18 +380,18 @@ static int max31335_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
320
380
regs [4 ] = bin2bcd (alrm -> time .tm_mon + 1 );
321
381
regs [5 ] = bin2bcd (alrm -> time .tm_year % 100 );
322
382
323
- ret = regmap_bulk_write (max31335 -> regmap , MAX31335_ALM1_SEC ,
383
+ ret = regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg ,
324
384
regs , sizeof (regs ));
325
385
if (ret )
326
386
return ret ;
327
387
328
388
reg = FIELD_PREP (MAX31335_INT_EN1_A1IE , alrm -> enabled );
329
- ret = regmap_update_bits (max31335 -> regmap , MAX31335_INT_EN1 ,
389
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_en_reg ,
330
390
MAX31335_INT_EN1_A1IE , reg );
331
391
if (ret )
332
392
return ret ;
333
393
334
- ret = regmap_update_bits (max31335 -> regmap , MAX31335_STATUS1 ,
394
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_status_reg ,
335
395
MAX31335_STATUS1_A1F , 0 );
336
396
337
397
return 0 ;
@@ -341,23 +401,33 @@ static int max31335_alarm_irq_enable(struct device *dev, unsigned int enabled)
341
401
{
342
402
struct max31335_data * max31335 = dev_get_drvdata (dev );
343
403
344
- return regmap_update_bits (max31335 -> regmap , MAX31335_INT_EN1 ,
404
+ return regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_en_reg ,
345
405
MAX31335_INT_EN1_A1IE , enabled );
346
406
}
347
407
348
408
static irqreturn_t max31335_handle_irq (int irq , void * dev_id )
349
409
{
350
410
struct max31335_data * max31335 = dev_id ;
351
- bool status ;
352
- int ret ;
411
+ struct mutex * lock = & max31335 -> rtc -> ops_lock ;
412
+ int ret , status ;
353
413
354
- ret = regmap_update_bits_check (max31335 -> regmap , MAX31335_STATUS1 ,
355
- MAX31335_STATUS1_A1F , 0 , & status );
414
+ mutex_lock (lock );
415
+
416
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
356
417
if (ret )
357
- return IRQ_HANDLED ;
418
+ goto exit ;
419
+
420
+ if (FIELD_GET (MAX31335_STATUS1_A1F , status )) {
421
+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_status_reg ,
422
+ MAX31335_STATUS1_A1F , 0 );
423
+ if (ret )
424
+ goto exit ;
358
425
359
- if (status )
360
426
rtc_update_irq (max31335 -> rtc , 1 , RTC_AF | RTC_IRQF );
427
+ }
428
+
429
+ exit :
430
+ mutex_unlock (lock );
361
431
362
432
return IRQ_HANDLED ;
363
433
}
@@ -404,7 +474,7 @@ static int max31335_trickle_charger_setup(struct device *dev,
404
474
405
475
i = i + trickle_cfg ;
406
476
407
- return regmap_write (max31335 -> regmap , MAX31335_TRICKLE_REG ,
477
+ return regmap_write (max31335 -> regmap , max31335 -> chip -> trickle_reg ,
408
478
FIELD_PREP (MAX31335_TRICKLE_REG_TRICKLE , i ) |
409
479
FIELD_PREP (MAX31335_TRICKLE_REG_EN_TRICKLE ,
410
480
chargeable ));
@@ -418,7 +488,7 @@ static unsigned long max31335_clkout_recalc_rate(struct clk_hw *hw,
418
488
unsigned int reg ;
419
489
int ret ;
420
490
421
- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
491
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
422
492
if (ret )
423
493
return 0 ;
424
494
@@ -449,23 +519,23 @@ static int max31335_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
449
519
ARRAY_SIZE (max31335_clkout_freq ));
450
520
freq_mask = __roundup_pow_of_two (ARRAY_SIZE (max31335_clkout_freq )) - 1 ;
451
521
452
- return regmap_update_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
453
- freq_mask , index );
522
+ return regmap_update_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
523
+ freq_mask , index );
454
524
}
455
525
456
526
static int max31335_clkout_enable (struct clk_hw * hw )
457
527
{
458
528
struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
459
529
460
- return regmap_set_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
461
- MAX31335_RTC_CONFIG2_ENCLKO );
530
+ return regmap_set_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
531
+ MAX31335_RTC_CONFIG2_ENCLKO );
462
532
}
463
533
464
534
static void max31335_clkout_disable (struct clk_hw * hw )
465
535
{
466
536
struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
467
537
468
- regmap_clear_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
538
+ regmap_clear_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
469
539
MAX31335_RTC_CONFIG2_ENCLKO );
470
540
}
471
541
@@ -475,7 +545,7 @@ static int max31335_clkout_is_enabled(struct clk_hw *hw)
475
545
unsigned int reg ;
476
546
int ret ;
477
547
478
- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
548
+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
479
549
if (ret )
480
550
return ret ;
481
551
@@ -500,7 +570,7 @@ static int max31335_nvmem_reg_read(void *priv, unsigned int offset,
500
570
void * val , size_t bytes )
501
571
{
502
572
struct max31335_data * max31335 = priv ;
503
- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
573
+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
504
574
505
575
return regmap_bulk_read (max31335 -> regmap , reg , val , bytes );
506
576
}
@@ -509,7 +579,7 @@ static int max31335_nvmem_reg_write(void *priv, unsigned int offset,
509
579
void * val , size_t bytes )
510
580
{
511
581
struct max31335_data * max31335 = priv ;
512
- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
582
+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
513
583
514
584
return regmap_bulk_write (max31335 -> regmap , reg , val , bytes );
515
585
}
@@ -533,7 +603,7 @@ static int max31335_read_temp(struct device *dev, enum hwmon_sensor_types type,
533
603
if (type != hwmon_temp || attr != hwmon_temp_input )
534
604
return - EOPNOTSUPP ;
535
605
536
- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_TEMP_DATA_MSB ,
606
+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> temp_reg ,
537
607
reg , 2 );
538
608
if (ret )
539
609
return ret ;
@@ -577,8 +647,8 @@ static int max31335_clkout_register(struct device *dev)
577
647
int ret ;
578
648
579
649
if (!device_property_present (dev , "#clock-cells" ))
580
- return regmap_clear_bits (max31335 -> regmap , MAX31335_RTC_CONFIG2 ,
581
- MAX31335_RTC_CONFIG2_ENCLKO );
650
+ return regmap_clear_bits (max31335 -> regmap , max31335 -> chip -> clkout_reg ,
651
+ MAX31335_RTC_CONFIG2_ENCLKO );
582
652
583
653
max31335 -> clkout .init = & max31335_clk_init ;
584
654
@@ -605,6 +675,7 @@ static int max31335_probe(struct i2c_client *client)
605
675
#if IS_REACHABLE (HWMON )
606
676
struct device * hwmon ;
607
677
#endif
678
+ const struct chip_desc * match ;
608
679
int ret ;
609
680
610
681
max31335 = devm_kzalloc (& client -> dev , sizeof (* max31335 ), GFP_KERNEL );
@@ -616,7 +687,10 @@ static int max31335_probe(struct i2c_client *client)
616
687
return PTR_ERR (max31335 -> regmap );
617
688
618
689
i2c_set_clientdata (client , max31335 );
619
-
690
+ match = i2c_get_match_data (client );
691
+ if (!match )
692
+ return - ENODEV ;
693
+ max31335 -> chip = match ;
620
694
max31335 -> rtc = devm_rtc_allocate_device (& client -> dev );
621
695
if (IS_ERR (max31335 -> rtc ))
622
696
return PTR_ERR (max31335 -> rtc );
@@ -639,6 +713,8 @@ static int max31335_probe(struct i2c_client *client)
639
713
dev_warn (& client -> dev ,
640
714
"unable to request IRQ, alarm max31335 disabled\n" );
641
715
client -> irq = 0 ;
716
+ } else {
717
+ max31335 -> irq = client -> irq ;
642
718
}
643
719
}
644
720
@@ -652,13 +728,13 @@ static int max31335_probe(struct i2c_client *client)
652
728
"cannot register rtc nvmem\n" );
653
729
654
730
#if IS_REACHABLE (HWMON )
655
- hwmon = devm_hwmon_device_register_with_info ( & client -> dev , client -> name ,
656
- max31335 ,
657
- & max31335_chip_info ,
658
- NULL );
659
- if ( IS_ERR (hwmon ))
660
- return dev_err_probe ( & client -> dev , PTR_ERR ( hwmon ),
661
- "cannot register hwmon device\n" );
731
+ if ( max31335 -> chip -> temp_reg ) {
732
+ hwmon = devm_hwmon_device_register_with_info ( & client -> dev , client -> name , max31335 ,
733
+ & max31335_chip_info , NULL );
734
+ if ( IS_ERR ( hwmon ))
735
+ return dev_err_probe ( & client -> dev , PTR_ERR (hwmon ),
736
+ "cannot register hwmon device\n" );
737
+ }
662
738
#endif
663
739
664
740
ret = max31335_trickle_charger_setup (& client -> dev , max31335 );
@@ -669,14 +745,16 @@ static int max31335_probe(struct i2c_client *client)
669
745
}
670
746
671
747
static const struct i2c_device_id max31335_id [] = {
672
- { "max31335" },
748
+ { "max31331" , (kernel_ulong_t )& chip [ID_MAX31331 ] },
749
+ { "max31335" , (kernel_ulong_t )& chip [ID_MAX31335 ] },
673
750
{ }
674
751
};
675
752
676
753
MODULE_DEVICE_TABLE (i2c , max31335_id );
677
754
678
755
static const struct of_device_id max31335_of_match [] = {
679
- { .compatible = "adi,max31335" },
756
+ { .compatible = "adi,max31331" , .data = & chip [ID_MAX31331 ] },
757
+ { .compatible = "adi,max31335" , .data = & chip [ID_MAX31335 ] },
680
758
{ }
681
759
};
682
760
@@ -693,5 +771,6 @@ static struct i2c_driver max31335_driver = {
693
771
module_i2c_driver (max31335_driver );
694
772
695
773
MODULE_AUTHOR ("Antoniu Miclaus <antoniu.miclaus@analog.com>" );
774
+ MODULE_AUTHOR ("Saket Kumar Purwar <Saket.Kumarpurwar@analog.com>" );
696
775
MODULE_DESCRIPTION ("MAX31335 RTC driver" );
697
776
MODULE_LICENSE ("GPL" );
0 commit comments