Lines Matching refs:chip

104 static int ug3105_get_status(struct ug3105_chip *chip)
106 int full = chip->info->constant_charge_voltage_max_uv - UG3105_FULL_BAT_HYST_UV;
108 if (chip->curr > UG3105_CURR_HYST_UA)
111 if (chip->curr < -UG3105_CURR_HYST_UA)
114 if (chip->supplied && chip->ocv_avg > full)
120 static int ug3105_get_capacity(struct ug3105_chip *chip)
154 if (chip->ocv_avg < ocv_capacity_tbl[0])
157 if (chip->status == POWER_SUPPLY_STATUS_FULL)
161 if (chip->ocv_avg > ocv_capacity_tbl[i])
164 ocv_diff = ocv_capacity_tbl[i] - chip->ocv_avg;
167 if (chip->info->constant_charge_voltage_max_uv >= 4300000)
178 struct ug3105_chip *chip = container_of(work, struct ug3105_chip,
181 bool prev_supplied = chip->supplied;
182 int prev_status = chip->status;
183 int prev_volt = chip->volt;
184 int prev_curr = chip->curr;
187 mutex_lock(&chip->lock);
189 psy = chip->psy;
193 val = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT);
196 chip->volt = val * chip->uv_per_unit;
198 val = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR);
201 chip->curr = (s16)val * chip->ua_per_unit;
203 chip->ocv[chip->ocv_avg_index] =
204 chip->volt - chip->curr * chip->intern_res_avg / 1000;
205 chip->ocv_avg_index = (chip->ocv_avg_index + 1) % UG3105_MOV_AVG_WINDOW;
206 chip->poll_count++;
212 * if ((chip->poll_count % 10) == 0) {
213 * val = ug3105_read_word(chip->client, UG3105_REG_COULOMB_CNT);
217 * i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1,
220 * chip->total_coulomb_count += (s16)val;
221 * dev_dbg(&chip->client->dev, "coulomb count %d total %d\n",
222 * (s16)val, chip->total_coulomb_count);
226 chip->ocv_avg = 0;
227 win_size = min(chip->poll_count, UG3105_MOV_AVG_WINDOW);
229 chip->ocv_avg += chip->ocv[i];
230 chip->ocv_avg /= win_size;
232 chip->supplied = power_supply_am_i_supplied(psy);
233 chip->status = ug3105_get_status(chip);
234 chip->capacity = ug3105_get_capacity(chip);
240 if (chip->supplied != prev_supplied ||
241 chip->volt < UG3105_LOW_BAT_UV ||
242 chip->poll_count < 2)
251 curr_diff = abs(chip->curr - prev_curr);
255 volt_diff = abs(chip->volt - prev_volt);
258 if ((res < (chip->intern_res_avg * 2 / 3)) ||
259 (res > (chip->intern_res_avg * 4 / 3))) {
260 dev_dbg(&chip->client->dev, "Ignoring outlier internal resistance %d mOhm\n", res);
264 dev_dbg(&chip->client->dev, "Internal resistance %d mOhm\n", res);
266 chip->intern_res[chip->intern_res_avg_index] = res;
267 chip->intern_res_avg_index = (chip->intern_res_avg_index + 1) % UG3105_MOV_AVG_WINDOW;
268 chip->intern_res_poll_count++;
270 chip->intern_res_avg = 0;
271 win_size = min(chip->intern_res_poll_count, UG3105_MOV_AVG_WINDOW);
273 chip->intern_res_avg += chip->intern_res[i];
274 chip->intern_res_avg /= win_size;
277 mutex_unlock(&chip->lock);
279 queue_delayed_work(system_wq, &chip->work,
280 (chip->poll_count <= UG3105_INIT_POLL_COUNT) ?
283 if (chip->status != prev_status && psy)
301 struct ug3105_chip *chip = power_supply_get_drvdata(psy);
304 mutex_lock(&chip->lock);
306 if (!chip->psy) {
313 val->intval = chip->status;
322 ret = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT);
325 val->intval = ret * chip->uv_per_unit;
329 val->intval = chip->ocv_avg;
332 ret = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR);
335 val->intval = (s16)ret * chip->ua_per_unit;
339 val->intval = chip->capacity;
346 mutex_unlock(&chip->lock);
352 struct ug3105_chip *chip = power_supply_get_drvdata(psy);
354 dev_dbg(&chip->client->dev, "external power changed\n");
355 mod_delayed_work(system_wq, &chip->work, UG3105_SETTLE_TIME);
367 static void ug3105_init(struct ug3105_chip *chip)
369 chip->poll_count = 0;
370 chip->ocv_avg_index = 0;
371 chip->total_coulomb_count = 0;
372 i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE,
374 i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1,
376 queue_delayed_work(system_wq, &chip->work, 0);
377 flush_delayed_work(&chip->work);
386 struct ug3105_chip *chip;
389 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
390 if (!chip)
393 chip->client = client;
394 mutex_init(&chip->lock);
395 ret = devm_delayed_work_autocancel(dev, &chip->work, ug3105_work);
399 psy_cfg.drv_data = chip;
404 ret = power_supply_get_battery_info(psy, &chip->info);
408 if (chip->info->factory_internal_resistance_uohm == -EINVAL ||
409 chip->info->constant_charge_voltage_max_uv == -EINVAL) {
420 chip->uv_per_unit = 45000000/65536;
422 chip->ua_per_unit = 8100000 / curr_sense_res_uohm;
425 chip->intern_res_avg = chip->info->factory_internal_resistance_uohm / 1000;
427 chip->intern_res[0] = chip->intern_res_avg;
428 chip->intern_res_avg_index = 1;
429 chip->intern_res_poll_count = 1;
431 mutex_lock(&chip->lock);
432 chip->psy = psy;
433 mutex_unlock(&chip->lock);
435 ug3105_init(chip);
437 i2c_set_clientdata(client, chip);
443 struct ug3105_chip *chip = dev_get_drvdata(dev);
445 cancel_delayed_work_sync(&chip->work);
446 i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE,
454 struct ug3105_chip *chip = dev_get_drvdata(dev);
456 ug3105_init(chip);