Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rx51_battery: convert to iio consumer

Update rx51-battery driver to use the new IIO API of
twl4030-madc and add DT support.

Signed-off-by: Sebastian Reichel <sre@kernel.org>

+66 -24
+66 -24
drivers/power/rx51_battery.c
··· 24 24 #include <linux/power_supply.h> 25 25 #include <linux/slab.h> 26 26 #include <linux/i2c/twl4030-madc.h> 27 - 28 - /* RX51 specific channels */ 29 - #define TWL4030_MADC_BTEMP_RX51 TWL4030_MADC_ADCIN0 30 - #define TWL4030_MADC_BCI_RX51 TWL4030_MADC_ADCIN4 27 + #include <linux/iio/consumer.h> 28 + #include <linux/of.h> 31 29 32 30 struct rx51_device_info { 33 31 struct device *dev; 34 32 struct power_supply bat; 33 + struct iio_channel *channel_temp; 34 + struct iio_channel *channel_bsi; 35 + struct iio_channel *channel_vbat; 35 36 }; 36 37 37 38 /* 38 39 * Read ADCIN channel value, code copied from maemo kernel 39 40 */ 40 - static int rx51_battery_read_adc(int channel) 41 + static int rx51_battery_read_adc(struct iio_channel *channel) 41 42 { 42 - struct twl4030_madc_request req; 43 - 44 - req.channels = channel; 45 - req.do_avg = 1; 46 - req.method = TWL4030_MADC_SW1; 47 - req.func_cb = NULL; 48 - req.type = TWL4030_MADC_WAIT; 49 - req.raw = true; 50 - 51 - if (twl4030_madc_conversion(&req) <= 0) 52 - return -ENODATA; 53 - 54 - return req.rbuf[ffs(channel) - 1]; 43 + int val, err; 44 + err = iio_read_channel_average_raw(channel, &val); 45 + if (err < 0) 46 + return err; 47 + return val; 55 48 } 56 49 57 50 /* ··· 53 60 */ 54 61 static int rx51_battery_read_voltage(struct rx51_device_info *di) 55 62 { 56 - int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT); 63 + int voltage = rx51_battery_read_adc(di->channel_vbat); 57 64 58 - if (voltage < 0) 65 + if (voltage < 0) { 66 + dev_err(di->dev, "Could not read ADC: %d\n", voltage); 59 67 return voltage; 68 + } 60 69 61 70 return 1000 * (10000 * voltage / 1705); 62 71 } ··· 107 112 { 108 113 int min = 0; 109 114 int max = ARRAY_SIZE(rx51_temp_table2) - 1; 110 - int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51); 115 + int raw = rx51_battery_read_adc(di->channel_temp); 116 + 117 + if (raw < 0) 118 + dev_err(di->dev, "Could not read ADC: %d\n", raw); 111 119 112 120 /* Zero and negative values are undefined */ 113 121 if (raw <= 0) ··· 144 146 */ 145 147 static int rx51_battery_read_capacity(struct rx51_device_info *di) 146 148 { 147 - int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51); 149 + int capacity = rx51_battery_read_adc(di->channel_bsi); 148 150 149 - if (capacity < 0) 151 + if (capacity < 0) { 152 + dev_err(di->dev, "Could not read ADC: %d\n", capacity); 150 153 return capacity; 154 + } 151 155 152 156 return 1280 * (1200 * capacity)/(1024 - capacity); 153 157 } ··· 213 213 214 214 platform_set_drvdata(pdev, di); 215 215 216 + di->dev = &pdev->dev; 216 217 di->bat.name = dev_name(&pdev->dev); 217 218 di->bat.type = POWER_SUPPLY_TYPE_BATTERY; 218 219 di->bat.properties = rx51_battery_props; 219 220 di->bat.num_properties = ARRAY_SIZE(rx51_battery_props); 220 221 di->bat.get_property = rx51_battery_get_property; 221 222 223 + di->channel_temp = iio_channel_get(di->dev, "temp"); 224 + if (IS_ERR(di->channel_temp)) { 225 + ret = PTR_ERR(di->channel_temp); 226 + goto error; 227 + } 228 + 229 + di->channel_bsi = iio_channel_get(di->dev, "bsi"); 230 + if (IS_ERR(di->channel_bsi)) { 231 + ret = PTR_ERR(di->channel_bsi); 232 + goto error_channel_temp; 233 + } 234 + 235 + di->channel_vbat = iio_channel_get(di->dev, "vbat"); 236 + if (IS_ERR(di->channel_vbat)) { 237 + ret = PTR_ERR(di->channel_vbat); 238 + goto error_channel_bsi; 239 + } 240 + 222 241 ret = power_supply_register(di->dev, &di->bat); 223 242 if (ret) 224 - return ret; 243 + goto error_channel_vbat; 225 244 226 245 return 0; 246 + 247 + error_channel_vbat: 248 + iio_channel_release(di->channel_vbat); 249 + error_channel_bsi: 250 + iio_channel_release(di->channel_bsi); 251 + error_channel_temp: 252 + iio_channel_release(di->channel_temp); 253 + error: 254 + 255 + return ret; 227 256 } 228 257 229 258 static int rx51_battery_remove(struct platform_device *pdev) ··· 261 232 262 233 power_supply_unregister(&di->bat); 263 234 235 + iio_channel_release(di->channel_vbat); 236 + iio_channel_release(di->channel_bsi); 237 + iio_channel_release(di->channel_temp); 238 + 264 239 return 0; 265 240 } 241 + 242 + #ifdef CONFIG_OF 243 + static const struct of_device_id n900_battery_of_match[] = { 244 + {.compatible = "nokia,n900-battery", }, 245 + { }, 246 + }; 247 + MODULE_DEVICE_TABLE(of, n900_battery_of_match); 248 + #endif 266 249 267 250 static struct platform_driver rx51_battery_driver = { 268 251 .probe = rx51_battery_probe, ··· 282 241 .driver = { 283 242 .name = "rx51-battery", 284 243 .owner = THIS_MODULE, 244 + .of_match_table = of_match_ptr(n900_battery_of_match), 285 245 }, 286 246 }; 287 247 module_platform_driver(rx51_battery_driver);