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

iio: adc: sc27xx: refactor some functions for support more PMiCs

Refactor the common adc_nvmem_cell_calib_data,adc_to_volt and call
these in the origin sc27xx_adc_scale_calibration,sc27xx_adc_to_volt

Signed-off-by: Cixi Geng <cixi.geng1@unisoc.com>
Link: https://lore.kernel.org/r/20220419142458.884933-6-gengcixi@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Cixi Geng and committed by
Jonathan Cameron
aac053d0 e4171388

+40 -16
+40 -16
drivers/iio/adc/sc27xx_adc.c
··· 135 135 return ((calib_data & 0xff) + calib_adc - 128) * 4; 136 136 } 137 137 138 + /* get the adc nvmem cell calibration data */ 139 + static int adc_nvmem_cell_calib_data(struct sc27xx_adc_data *data, const char *cell_name) 140 + { 141 + struct nvmem_cell *cell; 142 + void *buf; 143 + u32 origin_calib_data = 0; 144 + size_t len; 145 + 146 + if (!data) 147 + return -EINVAL; 148 + 149 + cell = nvmem_cell_get(data->dev, cell_name); 150 + if (IS_ERR(cell)) 151 + return PTR_ERR(cell); 152 + 153 + buf = nvmem_cell_read(cell, &len); 154 + if (IS_ERR(buf)) { 155 + nvmem_cell_put(cell); 156 + return PTR_ERR(buf); 157 + } 158 + 159 + memcpy(&origin_calib_data, buf, min(len, sizeof(u32))); 160 + 161 + kfree(buf); 162 + nvmem_cell_put(cell); 163 + return origin_calib_data; 164 + } 165 + 138 166 static int sc27xx_adc_scale_calibration(struct sc27xx_adc_data *data, 139 167 bool big_scale) 140 168 { 141 169 const struct sc27xx_adc_linear_graph *calib_graph; 142 170 struct sc27xx_adc_linear_graph *graph; 143 - struct nvmem_cell *cell; 144 171 const char *cell_name; 145 172 u32 calib_data = 0; 146 - void *buf; 147 - size_t len; 148 173 149 174 if (big_scale) { 150 175 calib_graph = data->var_data->bscale_cal; ··· 181 156 cell_name = "small_scale_calib"; 182 157 } 183 158 184 - cell = nvmem_cell_get(data->dev, cell_name); 185 - if (IS_ERR(cell)) 186 - return PTR_ERR(cell); 187 - 188 - buf = nvmem_cell_read(cell, &len); 189 - nvmem_cell_put(cell); 190 - 191 - if (IS_ERR(buf)) 192 - return PTR_ERR(buf); 193 - 194 - memcpy(&calib_data, buf, min(len, sizeof(u32))); 159 + calib_data = adc_nvmem_cell_calib_data(data, cell_name); 195 160 196 161 /* Only need to calibrate the adc values in the linear graph. */ 197 162 graph->adc0 = sc27xx_adc_get_calib_data(calib_data, calib_graph->adc0); 198 163 graph->adc1 = sc27xx_adc_get_calib_data(calib_data >> 8, 199 164 calib_graph->adc1); 200 165 201 - kfree(buf); 202 166 return 0; 203 167 } 204 168 ··· 323 309 *div_denominator = ratio & SC27XX_RATIO_DENOMINATOR_MASK; 324 310 } 325 311 326 - static int sc27xx_adc_to_volt(struct sc27xx_adc_linear_graph *graph, 312 + static int adc_to_volt(struct sc27xx_adc_linear_graph *graph, 327 313 int raw_adc) 328 314 { 329 315 int tmp; ··· 331 317 tmp = (graph->volt0 - graph->volt1) * (raw_adc - graph->adc1); 332 318 tmp /= (graph->adc0 - graph->adc1); 333 319 tmp += graph->volt1; 320 + 321 + return tmp; 322 + } 323 + 324 + static int sc27xx_adc_to_volt(struct sc27xx_adc_linear_graph *graph, 325 + int raw_adc) 326 + { 327 + int tmp; 328 + 329 + tmp = adc_to_volt(graph, raw_adc); 334 330 335 331 return tmp < 0 ? 0 : tmp; 336 332 }