16
16
#include <linux/i2c.h>
17
17
#include <linux/delay.h>
18
18
#include <linux/interrupt.h>
19
+ #include <linux/platform_device.h>
19
20
#include <linux/pm.h>
20
21
#include <linux/mod_devicetable.h>
21
22
#include <linux/power_supply.h>
52
53
#define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
53
54
54
55
struct max17042_chip {
55
- struct i2c_client * client ;
56
+ struct device * dev ;
56
57
struct regmap * regmap ;
57
58
struct power_supply * battery ;
58
59
enum max170xx_chip_type chip_type ;
59
60
struct max17042_platform_data * pdata ;
60
61
struct work_struct work ;
61
62
int init_complete ;
63
+ int irq ;
62
64
};
63
65
64
66
static enum power_supply_property max17042_battery_props [] = {
@@ -573,11 +575,11 @@ static inline int max17042_model_data_compare(struct max17042_chip *chip,
573
575
int i ;
574
576
575
577
if (memcmp (data1 , data2 , size )) {
576
- dev_err (& chip -> client -> dev , "%s compare failed\n" , __func__ );
578
+ dev_err (chip -> dev , "%s compare failed\n" , __func__ );
577
579
for (i = 0 ; i < size ; i ++ )
578
- dev_info (& chip -> client -> dev , "0x%x, 0x%x" ,
580
+ dev_info (chip -> dev , "0x%x, 0x%x" ,
579
581
data1 [i ], data2 [i ]);
580
- dev_info (& chip -> client -> dev , "\n" );
582
+ dev_info (chip -> dev , "\n" );
581
583
return - EINVAL ;
582
584
}
583
585
return 0 ;
@@ -812,14 +814,14 @@ static int max17042_init_chip(struct max17042_chip *chip)
812
814
/* write cell characterization data */
813
815
ret = max17042_init_model (chip );
814
816
if (ret ) {
815
- dev_err (& chip -> client -> dev , "%s init failed\n" ,
817
+ dev_err (chip -> dev , "%s init failed\n" ,
816
818
__func__ );
817
819
return - EIO ;
818
820
}
819
821
820
822
ret = max17042_verify_model_lock (chip );
821
823
if (ret ) {
822
- dev_err (& chip -> client -> dev , "%s lock verify failed\n" ,
824
+ dev_err (chip -> dev , "%s lock verify failed\n" ,
823
825
__func__ );
824
826
return - EIO ;
825
827
}
@@ -875,7 +877,7 @@ static irqreturn_t max17042_thread_handler(int id, void *dev)
875
877
return IRQ_HANDLED ;
876
878
877
879
if ((val & STATUS_SMN_BIT ) || (val & STATUS_SMX_BIT )) {
878
- dev_dbg (& chip -> client -> dev , "SOC threshold INTR\n" );
880
+ dev_dbg (chip -> dev , "SOC threshold INTR\n" );
879
881
max17042_set_soc_threshold (chip , 1 );
880
882
}
881
883
@@ -907,7 +909,7 @@ static void max17042_init_worker(struct work_struct *work)
907
909
static struct max17042_platform_data *
908
910
max17042_get_of_pdata (struct max17042_chip * chip )
909
911
{
910
- struct device * dev = & chip -> client -> dev ;
912
+ struct device * dev = chip -> dev ;
911
913
struct device_node * np = dev -> of_node ;
912
914
u32 prop ;
913
915
struct max17042_platform_data * pdata ;
@@ -949,7 +951,7 @@ static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
949
951
static struct max17042_platform_data *
950
952
max17042_get_default_pdata (struct max17042_chip * chip )
951
953
{
952
- struct device * dev = & chip -> client -> dev ;
954
+ struct device * dev = chip -> dev ;
953
955
struct max17042_platform_data * pdata ;
954
956
int ret , misc_cfg ;
955
957
@@ -990,7 +992,7 @@ max17042_get_default_pdata(struct max17042_chip *chip)
990
992
static struct max17042_platform_data *
991
993
max17042_get_pdata (struct max17042_chip * chip )
992
994
{
993
- struct device * dev = & chip -> client -> dev ;
995
+ struct device * dev = chip -> dev ;
994
996
995
997
#ifdef CONFIG_OF
996
998
if (dev -> of_node )
@@ -1003,6 +1005,7 @@ max17042_get_pdata(struct max17042_chip *chip)
1003
1005
}
1004
1006
1005
1007
static const struct regmap_config max17042_regmap_config = {
1008
+ .name = "max17042" ,
1006
1009
.reg_bits = 8 ,
1007
1010
.val_bits = 16 ,
1008
1011
.val_format_endian = REGMAP_ENDIAN_NATIVE ,
@@ -1029,14 +1032,12 @@ static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
1029
1032
.num_properties = ARRAY_SIZE (max17042_battery_props ) - 2 ,
1030
1033
};
1031
1034
1032
- static int max17042_probe (struct i2c_client * client )
1035
+ static int max17042_probe (struct i2c_client * client , struct device * dev , int irq ,
1036
+ enum max170xx_chip_type chip_type )
1033
1037
{
1034
- const struct i2c_device_id * id = i2c_client_get_device_id (client );
1035
1038
struct i2c_adapter * adapter = client -> adapter ;
1036
1039
const struct power_supply_desc * max17042_desc = & max17042_psy_desc ;
1037
1040
struct power_supply_config psy_cfg = {};
1038
- const struct acpi_device_id * acpi_id = NULL ;
1039
- struct device * dev = & client -> dev ;
1040
1041
struct max17042_chip * chip ;
1041
1042
int ret ;
1042
1043
int i ;
@@ -1045,33 +1046,25 @@ static int max17042_probe(struct i2c_client *client)
1045
1046
if (!i2c_check_functionality (adapter , I2C_FUNC_SMBUS_WORD_DATA ))
1046
1047
return - EIO ;
1047
1048
1048
- chip = devm_kzalloc (& client -> dev , sizeof (* chip ), GFP_KERNEL );
1049
+ chip = devm_kzalloc (dev , sizeof (* chip ), GFP_KERNEL );
1049
1050
if (!chip )
1050
1051
return - ENOMEM ;
1051
1052
1052
- chip -> client = client ;
1053
- if (id ) {
1054
- chip -> chip_type = id -> driver_data ;
1055
- } else {
1056
- acpi_id = acpi_match_device (dev -> driver -> acpi_match_table , dev );
1057
- if (!acpi_id )
1058
- return - ENODEV ;
1059
-
1060
- chip -> chip_type = acpi_id -> driver_data ;
1061
- }
1053
+ chip -> dev = dev ;
1054
+ chip -> chip_type = chip_type ;
1062
1055
chip -> regmap = devm_regmap_init_i2c (client , & max17042_regmap_config );
1063
1056
if (IS_ERR (chip -> regmap )) {
1064
- dev_err (& client -> dev , "Failed to initialize regmap\n" );
1057
+ dev_err (dev , "Failed to initialize regmap\n" );
1065
1058
return - EINVAL ;
1066
1059
}
1067
1060
1068
1061
chip -> pdata = max17042_get_pdata (chip );
1069
1062
if (!chip -> pdata ) {
1070
- dev_err (& client -> dev , "no platform data provided\n" );
1063
+ dev_err (dev , "no platform data provided\n" );
1071
1064
return - EINVAL ;
1072
1065
}
1073
1066
1074
- i2c_set_clientdata ( client , chip );
1067
+ dev_set_drvdata ( dev , chip );
1075
1068
psy_cfg .drv_data = chip ;
1076
1069
psy_cfg .of_node = dev -> of_node ;
1077
1070
@@ -1095,17 +1088,17 @@ static int max17042_probe(struct i2c_client *client)
1095
1088
regmap_write (chip -> regmap , MAX17042_LearnCFG , 0x0007 );
1096
1089
}
1097
1090
1098
- chip -> battery = devm_power_supply_register (& client -> dev , max17042_desc ,
1091
+ chip -> battery = devm_power_supply_register (dev , max17042_desc ,
1099
1092
& psy_cfg );
1100
1093
if (IS_ERR (chip -> battery )) {
1101
- dev_err (& client -> dev , "failed: power supply register\n" );
1094
+ dev_err (dev , "failed: power supply register\n" );
1102
1095
return PTR_ERR (chip -> battery );
1103
1096
}
1104
1097
1105
- if (client -> irq ) {
1098
+ if (irq ) {
1106
1099
unsigned int flags = IRQF_ONESHOT | IRQF_SHARED | IRQF_PROBE_SHARED ;
1107
1100
1108
- ret = devm_request_threaded_irq (& client -> dev , client -> irq ,
1101
+ ret = devm_request_threaded_irq (dev , irq ,
1109
1102
NULL ,
1110
1103
max17042_thread_handler , flags ,
1111
1104
chip -> battery -> desc -> name ,
@@ -1116,18 +1109,20 @@ static int max17042_probe(struct i2c_client *client)
1116
1109
CFG_ALRT_BIT_ENBL );
1117
1110
max17042_set_soc_threshold (chip , 1 );
1118
1111
} else {
1119
- client -> irq = 0 ;
1112
+ irq = 0 ;
1120
1113
if (ret != - EBUSY )
1121
- dev_err (& client -> dev , "Failed to get IRQ\n" );
1114
+ dev_err (dev , "Failed to get IRQ\n" );
1122
1115
}
1123
1116
}
1124
1117
/* Not able to update the charge threshold when exceeded? -> disable */
1125
- if (!client -> irq )
1118
+ if (!irq )
1126
1119
regmap_write (chip -> regmap , MAX17042_SALRT_Th , 0xff00 );
1127
1120
1121
+ chip -> irq = irq ;
1122
+
1128
1123
regmap_read (chip -> regmap , MAX17042_STATUS , & val );
1129
1124
if (val & STATUS_POR_BIT ) {
1130
- ret = devm_work_autocancel (& client -> dev , & chip -> work ,
1125
+ ret = devm_work_autocancel (dev , & chip -> work ,
1131
1126
max17042_init_worker );
1132
1127
if (ret )
1133
1128
return ret ;
@@ -1139,6 +1134,44 @@ static int max17042_probe(struct i2c_client *client)
1139
1134
return 0 ;
1140
1135
}
1141
1136
1137
+ static int max17042_i2c_probe (struct i2c_client * client )
1138
+ {
1139
+ const struct i2c_device_id * id = i2c_client_get_device_id (client );
1140
+ const struct acpi_device_id * acpi_id = NULL ;
1141
+ struct device * dev = & client -> dev ;
1142
+ enum max170xx_chip_type chip_type ;
1143
+
1144
+ if (id ) {
1145
+ chip_type = id -> driver_data ;
1146
+ } else {
1147
+ acpi_id = acpi_match_device (dev -> driver -> acpi_match_table , dev );
1148
+ if (!acpi_id )
1149
+ return - ENODEV ;
1150
+
1151
+ chip_type = acpi_id -> driver_data ;
1152
+ }
1153
+
1154
+ return max17042_probe (client , dev , client -> irq , chip_type );
1155
+ }
1156
+
1157
+ static int max17042_platform_probe (struct platform_device * pdev )
1158
+ {
1159
+ struct device * dev = & pdev -> dev ;
1160
+ struct i2c_client * i2c ;
1161
+ const struct platform_device_id * id ;
1162
+ int irq ;
1163
+
1164
+ i2c = to_i2c_client (pdev -> dev .parent );
1165
+ if (!i2c )
1166
+ return - EINVAL ;
1167
+
1168
+ dev -> of_node = dev -> parent -> of_node ;
1169
+ id = platform_get_device_id (pdev );
1170
+ irq = platform_get_irq (pdev , 0 );
1171
+
1172
+ return max17042_probe (i2c , dev , irq , id -> driver_data );
1173
+ }
1174
+
1142
1175
#ifdef CONFIG_PM_SLEEP
1143
1176
static int max17042_suspend (struct device * dev )
1144
1177
{
@@ -1148,9 +1181,9 @@ static int max17042_suspend(struct device *dev)
1148
1181
* disable the irq and enable irq_wake
1149
1182
* capability to the interrupt line.
1150
1183
*/
1151
- if (chip -> client -> irq ) {
1152
- disable_irq (chip -> client -> irq );
1153
- enable_irq_wake (chip -> client -> irq );
1184
+ if (chip -> irq ) {
1185
+ disable_irq (chip -> irq );
1186
+ enable_irq_wake (chip -> irq );
1154
1187
}
1155
1188
1156
1189
return 0 ;
@@ -1160,9 +1193,9 @@ static int max17042_resume(struct device *dev)
1160
1193
{
1161
1194
struct max17042_chip * chip = dev_get_drvdata (dev );
1162
1195
1163
- if (chip -> client -> irq ) {
1164
- disable_irq_wake (chip -> client -> irq );
1165
- enable_irq (chip -> client -> irq );
1196
+ if (chip -> irq ) {
1197
+ disable_irq_wake (chip -> irq );
1198
+ enable_irq (chip -> irq );
1166
1199
/* re-program the SOC thresholds to 1% change */
1167
1200
max17042_set_soc_threshold (chip , 1 );
1168
1201
}
@@ -1183,12 +1216,26 @@ MODULE_DEVICE_TABLE(acpi, max17042_acpi_match);
1183
1216
#endif
1184
1217
1185
1218
#ifdef CONFIG_OF
1186
- static const struct of_device_id max17042_dt_match [] = {
1187
- { .compatible = "maxim,max17042" },
1188
- { .compatible = "maxim,max17047" },
1189
- { .compatible = "maxim,max17050" },
1190
- { .compatible = "maxim,max17055" },
1191
- { .compatible = "maxim,max77849-battery" },
1219
+ /*
1220
+ * Device may be instantiated through parent MFD device and device matching is done
1221
+ * through platform_device_id.
1222
+ *
1223
+ * However if device's DT node contains proper clock compatible and driver is
1224
+ * built as a module, then the *module* matching will be done trough DT aliases.
1225
+ * This requires of_device_id table. In the same time this will not change the
1226
+ * actual *device* matching so do not add .of_match_table.
1227
+ */
1228
+ static const struct of_device_id max17042_dt_match [] __used = {
1229
+ { .compatible = "maxim,max17042" ,
1230
+ .data = (void * ) MAXIM_DEVICE_TYPE_MAX17042 },
1231
+ { .compatible = "maxim,max17047" ,
1232
+ .data = (void * ) MAXIM_DEVICE_TYPE_MAX17047 },
1233
+ { .compatible = "maxim,max17050" ,
1234
+ .data = (void * ) MAXIM_DEVICE_TYPE_MAX17050 },
1235
+ { .compatible = "maxim,max17055" ,
1236
+ .data = (void * ) MAXIM_DEVICE_TYPE_MAX17055 },
1237
+ { .compatible = "maxim,max77849-battery" ,
1238
+ .data = (void * ) MAXIM_DEVICE_TYPE_MAX17047 },
1192
1239
{ },
1193
1240
};
1194
1241
MODULE_DEVICE_TABLE (of , max17042_dt_match );
@@ -1204,17 +1251,61 @@ static const struct i2c_device_id max17042_id[] = {
1204
1251
};
1205
1252
MODULE_DEVICE_TABLE (i2c , max17042_id );
1206
1253
1254
+ static const struct platform_device_id max17042_platform_id [] = {
1255
+ { "max17042" , MAXIM_DEVICE_TYPE_MAX17042 },
1256
+ { "max17047" , MAXIM_DEVICE_TYPE_MAX17047 },
1257
+ { "max17050" , MAXIM_DEVICE_TYPE_MAX17050 },
1258
+ { "max17055" , MAXIM_DEVICE_TYPE_MAX17055 },
1259
+ { "max77849-battery" , MAXIM_DEVICE_TYPE_MAX17047 },
1260
+ { }
1261
+ };
1262
+ MODULE_DEVICE_TABLE (platform , max17042_platform_id );
1263
+
1207
1264
static struct i2c_driver max17042_i2c_driver = {
1208
1265
.driver = {
1209
1266
.name = "max17042" ,
1210
1267
.acpi_match_table = ACPI_PTR (max17042_acpi_match ),
1211
1268
.of_match_table = of_match_ptr (max17042_dt_match ),
1212
1269
.pm = & max17042_pm_ops ,
1213
1270
},
1214
- .probe = max17042_probe ,
1271
+ .probe = max17042_i2c_probe ,
1215
1272
.id_table = max17042_id ,
1216
1273
};
1217
- module_i2c_driver (max17042_i2c_driver );
1274
+
1275
+ static struct platform_driver max17042_platform_driver = {
1276
+ .driver = {
1277
+ .name = "max17042" ,
1278
+ .acpi_match_table = ACPI_PTR (max17042_acpi_match ),
1279
+ .pm = & max17042_pm_ops ,
1280
+ },
1281
+ .probe = max17042_platform_probe ,
1282
+ .id_table = max17042_platform_id ,
1283
+ };
1284
+
1285
+ static int __init max17042_init (void )
1286
+ {
1287
+ int ret ;
1288
+
1289
+ ret = platform_driver_register (& max17042_platform_driver );
1290
+ if (ret )
1291
+ return ret ;
1292
+
1293
+ ret = i2c_add_driver (& max17042_i2c_driver );
1294
+ if (ret ) {
1295
+ platform_driver_unregister (& max17042_platform_driver );
1296
+ return ret ;
1297
+ }
1298
+
1299
+ return 0 ;
1300
+ }
1301
+ module_init (max17042_init );
1302
+
1303
+ static void __exit max17042_exit (void )
1304
+ {
1305
+ i2c_del_driver (& max17042_i2c_driver );
1306
+ platform_driver_unregister (& max17042_platform_driver );
1307
+ }
1308
+ module_exit (max17042_exit );
1218
1309
1219
1310
MODULE_AUTHOR (
"MyungJoo Ham <[email protected] >" );
1220
1311
MODULE_DESCRIPTION ("MAX17042 Fuel Gauge" );
0 commit comments