184184#define MAX31335_RAM_SIZE 32
185185#define MAX31335_TIME_SIZE 0x07
186186
187+ /* MAX31331 Register Map */
188+ #define MAX31331_RTC_CONFIG2 0x04
189+
187190#define clk_hw_to_max31335 (_hw ) container_of(_hw, struct max31335_data, clkout)
188191
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+
189216struct max31335_data {
217+ enum max_rtc_ids id ;
190218 struct regmap * regmap ;
191219 struct rtc_device * rtc ;
192220 struct clk_hw clkout ;
221+ struct clk * clkin ;
222+ const struct chip_desc * chip ;
223+ int irq ;
193224};
194225
195226static const int max31335_clkout_freq [] = { 1 , 64 , 1024 , 32768 };
196227
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+
197252static const u16 max31335_trickle_resistors [] = {3000 , 6000 , 11000 };
198253
199254static bool max31335_volatile_reg (struct device * dev , unsigned int reg )
200255{
256+ struct max31335_data * max31335 = dev_get_drvdata (dev );
257+ const struct chip_desc * chip = max31335 -> chip ;
258+
201259 /* 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 )
204262 return true;
205263
206264 /* interrupt status register */
207- if (reg == MAX31335_STATUS1 )
265+ if (reg == chip -> int_status_reg )
208266 return true;
209267
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 ))
212271 return true;
213272
214273 return false;
@@ -227,7 +286,7 @@ static int max31335_read_time(struct device *dev, struct rtc_time *tm)
227286 u8 date [7 ];
228287 int ret ;
229288
230- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_SECONDS , date ,
289+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
231290 sizeof (date ));
232291 if (ret )
233292 return ret ;
@@ -262,7 +321,7 @@ static int max31335_set_time(struct device *dev, struct rtc_time *tm)
262321 if (tm -> tm_year >= 200 )
263322 date [5 ] |= FIELD_PREP (MAX31335_MONTH_CENTURY , 1 );
264323
265- return regmap_bulk_write (max31335 -> regmap , MAX31335_SECONDS , date ,
324+ return regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> sec_reg , date ,
266325 sizeof (date ));
267326}
268327
@@ -273,7 +332,7 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
273332 struct rtc_time time ;
274333 u8 regs [6 ];
275334
276- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_ALM1_SEC , regs ,
335+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg , regs ,
277336 sizeof (regs ));
278337 if (ret )
279338 return ret ;
@@ -292,11 +351,11 @@ static int max31335_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
292351 if (time .tm_year >= 200 )
293352 alrm -> time .tm_year += 100 ;
294353
295- ret = regmap_read (max31335 -> regmap , MAX31335_INT_EN1 , & ctrl );
354+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_en_reg , & ctrl );
296355 if (ret )
297356 return ret ;
298357
299- ret = regmap_read (max31335 -> regmap , MAX31335_STATUS1 , & status );
358+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
300359 if (ret )
301360 return ret ;
302361
@@ -320,18 +379,18 @@ static int max31335_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
320379 regs [4 ] = bin2bcd (alrm -> time .tm_mon + 1 );
321380 regs [5 ] = bin2bcd (alrm -> time .tm_year % 100 );
322381
323- ret = regmap_bulk_write (max31335 -> regmap , MAX31335_ALM1_SEC ,
382+ ret = regmap_bulk_write (max31335 -> regmap , max31335 -> chip -> alarm1_sec_reg ,
324383 regs , sizeof (regs ));
325384 if (ret )
326385 return ret ;
327386
328387 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 ,
330389 MAX31335_INT_EN1_A1IE , reg );
331390 if (ret )
332391 return ret ;
333392
334- ret = regmap_update_bits (max31335 -> regmap , MAX31335_STATUS1 ,
393+ ret = regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_status_reg ,
335394 MAX31335_STATUS1_A1F , 0 );
336395
337396 return 0 ;
@@ -341,7 +400,7 @@ static int max31335_alarm_irq_enable(struct device *dev, unsigned int enabled)
341400{
342401 struct max31335_data * max31335 = dev_get_drvdata (dev );
343402
344- return regmap_update_bits (max31335 -> regmap , MAX31335_INT_EN1 ,
403+ return regmap_update_bits (max31335 -> regmap , max31335 -> chip -> int_en_reg ,
345404 MAX31335_INT_EN1_A1IE , enabled );
346405}
347406
@@ -353,12 +412,12 @@ static irqreturn_t max31335_handle_irq(int irq, void *dev_id)
353412
354413 mutex_lock (lock );
355414
356- ret = regmap_read (max31335 -> regmap , MAX31335_STATUS1 , & status );
415+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> int_status_reg , & status );
357416 if (ret )
358417 goto exit ;
359418
360419 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 ,
362421 MAX31335_STATUS1_A1F , 0 );
363422 if (ret )
364423 goto exit ;
@@ -414,7 +473,7 @@ static int max31335_trickle_charger_setup(struct device *dev,
414473
415474 i = i + trickle_cfg ;
416475
417- return regmap_write (max31335 -> regmap , MAX31335_TRICKLE_REG ,
476+ return regmap_write (max31335 -> regmap , max31335 -> chip -> trickle_reg ,
418477 FIELD_PREP (MAX31335_TRICKLE_REG_TRICKLE , i ) |
419478 FIELD_PREP (MAX31335_TRICKLE_REG_EN_TRICKLE ,
420479 chargeable ));
@@ -428,7 +487,7 @@ static unsigned long max31335_clkout_recalc_rate(struct clk_hw *hw,
428487 unsigned int reg ;
429488 int ret ;
430489
431- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
490+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
432491 if (ret )
433492 return 0 ;
434493
@@ -454,29 +513,32 @@ static int max31335_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
454513 struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
455514 unsigned int freq_mask ;
456515 int index ;
516+ int ret ;
457517
458518 index = find_closest (rate , max31335_clkout_freq ,
459519 ARRAY_SIZE (max31335_clkout_freq ));
460520 freq_mask = __roundup_pow_of_two (ARRAY_SIZE (max31335_clkout_freq )) - 1 ;
461521
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 ;
464526}
465527
466528static int max31335_clkout_enable (struct clk_hw * hw )
467529{
468530 struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
469531
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 );
472534}
473535
474536static void max31335_clkout_disable (struct clk_hw * hw )
475537{
476538 struct max31335_data * max31335 = clk_hw_to_max31335 (hw );
477539
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 );
480542}
481543
482544static int max31335_clkout_is_enabled (struct clk_hw * hw )
@@ -485,7 +547,7 @@ static int max31335_clkout_is_enabled(struct clk_hw *hw)
485547 unsigned int reg ;
486548 int ret ;
487549
488- ret = regmap_read (max31335 -> regmap , MAX31335_RTC_CONFIG2 , & reg );
550+ ret = regmap_read (max31335 -> regmap , max31335 -> chip -> clkout_reg , & reg );
489551 if (ret )
490552 return ret ;
491553
@@ -510,7 +572,7 @@ static int max31335_nvmem_reg_read(void *priv, unsigned int offset,
510572 void * val , size_t bytes )
511573{
512574 struct max31335_data * max31335 = priv ;
513- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
575+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
514576
515577 return regmap_bulk_read (max31335 -> regmap , reg , val , bytes );
516578}
@@ -519,7 +581,7 @@ static int max31335_nvmem_reg_write(void *priv, unsigned int offset,
519581 void * val , size_t bytes )
520582{
521583 struct max31335_data * max31335 = priv ;
522- unsigned int reg = MAX31335_TS0_SEC_1_128 + offset ;
584+ unsigned int reg = max31335 -> chip -> ram_reg + offset ;
523585
524586 return regmap_bulk_write (max31335 -> regmap , reg , val , bytes );
525587}
@@ -543,7 +605,7 @@ static int max31335_read_temp(struct device *dev, enum hwmon_sensor_types type,
543605 if (type != hwmon_temp || attr != hwmon_temp_input )
544606 return - EOPNOTSUPP ;
545607
546- ret = regmap_bulk_read (max31335 -> regmap , MAX31335_TEMP_DATA_MSB ,
608+ ret = regmap_bulk_read (max31335 -> regmap , max31335 -> chip -> temp_reg ,
547609 reg , 2 );
548610 if (ret )
549611 return ret ;
@@ -586,9 +648,10 @@ static int max31335_clkout_register(struct device *dev)
586648 struct max31335_data * max31335 = dev_get_drvdata (dev );
587649 int ret ;
588650
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+ }
592655
593656 max31335 -> clkout .init = & max31335_clk_init ;
594657
@@ -617,6 +680,7 @@ static int max31335_probe(struct i2c_client *client,
617680#if IS_REACHABLE (HWMON )
618681 struct device * hwmon ;
619682#endif
683+ const void * match ;
620684 int ret ;
621685
622686 max31335 = devm_kzalloc (& client -> dev , sizeof (* max31335 ), GFP_KERNEL );
@@ -629,6 +693,16 @@ static int max31335_probe(struct i2c_client *client,
629693
630694 i2c_set_clientdata (client , max31335 );
631695
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+
632706 max31335 -> rtc = devm_rtc_allocate_device (& client -> dev );
633707 if (IS_ERR (max31335 -> rtc ))
634708 return PTR_ERR (max31335 -> rtc );
@@ -651,6 +725,8 @@ static int max31335_probe(struct i2c_client *client,
651725 dev_warn (& client -> dev ,
652726 "unable to request IRQ, alarm max31335 disabled\n" );
653727 client -> irq = 0 ;
728+ } else {
729+ max31335 -> irq = client -> irq ;
654730 }
655731 }
656732
@@ -664,13 +740,15 @@ static int max31335_probe(struct i2c_client *client,
664740 "cannot register rtc nvmem\n" );
665741
666742#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+ }
674752#endif
675753
676754 ret = max31335_trickle_charger_setup (& client -> dev , max31335 );
@@ -681,14 +759,16 @@ static int max31335_probe(struct i2c_client *client,
681759}
682760
683761static 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 ] },
685764 { }
686765};
687766
688767MODULE_DEVICE_TABLE (i2c , max31335_id );
689768
690769static 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 ] },
692772 { }
693773};
694774
@@ -705,5 +785,6 @@ static struct i2c_driver max31335_driver = {
705785module_i2c_driver (max31335_driver );
706786
707787MODULE_AUTHOR ("Antoniu Miclaus <antoniu.miclaus@analog.com>" );
788+ MODULE_AUTHOR ("Saket Kumar Purwar <Saket.Kumarpurwar@analog.com>" );
708789MODULE_DESCRIPTION ("MAX31335 RTC driver" );
709790MODULE_LICENSE ("GPL" );
0 commit comments