Lines Matching +full:device +full:- +full:chemistry

4  * SPDX-License-Identifier: Apache-2.0
7 * - BQ27441
8 * Datasheet: https://www.ti.com/lit/gpn/bq27441-g1
10 * - BQ27421
11 * Datasheet: https://www.ti.com/lit/gpn/bq27421-g1
13 * - BQ27427
23 #include <zephyr/pm/device.h>
47 * defined as 250ms typical in the datasheet (Power-up communication delay).
82 static int bq274xx_cmd_reg_read(const struct device *dev, uint8_t reg_addr, in bq274xx_cmd_reg_read()
85 const struct bq274xx_config *config = dev->config; in bq274xx_cmd_reg_read()
89 ret = i2c_burst_read_dt(&config->i2c, reg_addr, i2c_data, sizeof(i2c_data)); in bq274xx_cmd_reg_read()
92 return -EIO; in bq274xx_cmd_reg_read()
100 static int bq274xx_ctrl_reg_write(const struct device *dev, uint16_t subcommand) in bq274xx_ctrl_reg_write()
102 const struct bq274xx_config *config = dev->config; in bq274xx_ctrl_reg_write()
110 ret = i2c_write_dt(&config->i2c, tx_buf, sizeof(tx_buf)); in bq274xx_ctrl_reg_write()
113 return -EIO; in bq274xx_ctrl_reg_write()
119 static int bq274xx_get_device_type(const struct device *dev, uint16_t *val) in bq274xx_get_device_type()
126 return -EIO; in bq274xx_get_device_type()
132 return -EIO; in bq274xx_get_device_type()
138 static int bq274xx_read_block(const struct device *dev, in bq274xx_read_block()
142 const struct bq274xx_config *const config = dev->config; in bq274xx_read_block()
145 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_DATA_CLASS, subclass); in bq274xx_read_block()
148 return -EIO; in bq274xx_read_block()
152 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_DATA_BLOCK, 0x00); in bq274xx_read_block()
155 return -EIO; in bq274xx_read_block()
160 ret = i2c_burst_read_dt(&config->i2c, BQ274XX_EXT_BLKDAT_START, block, num_bytes); in bq274xx_read_block()
163 return -EIO; in bq274xx_read_block()
169 static int bq274xx_write_block(const struct device *dev, in bq274xx_write_block()
172 const struct bq274xx_config *const config = dev->config; in bq274xx_write_block()
182 ret = i2c_write_dt(&config->i2c, buf, 1 + num_bytes); in bq274xx_write_block()
185 return -EIO; in bq274xx_write_block()
191 checksum = 0xff - checksum; in bq274xx_write_block()
193 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_CHECKSUM, checksum); in bq274xx_write_block()
196 return -EIO; in bq274xx_write_block()
223 static int bq274xx_mode_cfgupdate(const struct device *dev, bool enabled) in bq274xx_mode_cfgupdate()
233 LOG_ERR("Unable to set device mode to %02x", val); in bq274xx_mode_cfgupdate()
234 return -EIO; in bq274xx_mode_cfgupdate()
241 return -EIO; in bq274xx_mode_cfgupdate()
255 return -EIO; in bq274xx_mode_cfgupdate()
264 …ttps://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/1215460…
266 static int bq27427_ccgain_quirk(const struct device *dev) in bq27427_ccgain_quirk()
268 const struct bq274xx_config *const config = dev->config; in bq27427_ccgain_quirk()
272 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_DATA_CLASS, in bq27427_ccgain_quirk()
276 return -EIO; in bq27427_ccgain_quirk()
279 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_DATA_BLOCK, 0x00); in bq27427_ccgain_quirk()
282 return -EIO; in bq27427_ccgain_quirk()
287 ret = i2c_reg_read_byte_dt(&config->i2c, BQ27427_CC_GAIN, &val); in bq27427_ccgain_quirk()
290 return -EIO; in bq27427_ccgain_quirk()
298 ret = i2c_reg_read_byte_dt(&config->i2c, BQ274XX_EXT_CHECKSUM, &checksum); in bq27427_ccgain_quirk()
301 return -EIO; in bq27427_ccgain_quirk()
310 ret = i2c_reg_write_byte_dt(&config->i2c, BQ27427_CC_GAIN, val); in bq27427_ccgain_quirk()
313 return -EIO; in bq27427_ccgain_quirk()
316 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_CHECKSUM, checksum); in bq27427_ccgain_quirk()
319 return -EIO; in bq27427_ccgain_quirk()
327 static int bq274xx_ensure_chemistry(const struct device *dev) in bq274xx_ensure_chemistry()
329 struct bq274xx_data *data = dev->data; in bq274xx_ensure_chemistry()
330 const struct bq274xx_config *const config = dev->config; in bq274xx_ensure_chemistry()
331 uint16_t chem_id = config->chemistry_id; in bq274xx_ensure_chemistry()
334 /* No chemistry ID set, rely on the default of the device.*/ in bq274xx_ensure_chemistry()
343 return -EIO; in bq274xx_ensure_chemistry()
349 return -EIO; in bq274xx_ensure_chemistry()
355 /* Only the bq27427 has a configurable Chemistry ID. On bq27421, it depends on the in bq274xx_ensure_chemistry()
356 * variant of the chip, so just error out if the chemistry ID is wrong. in bq274xx_ensure_chemistry()
358 if (data->regs != &bq27427_regs) { in bq274xx_ensure_chemistry()
359 LOG_ERR("Unable to confirm chemistry ID 0x%04x. Device reported 0x%04x", in bq274xx_ensure_chemistry()
361 return -EIO; in bq274xx_ensure_chemistry()
377 LOG_ERR("Unsupported chemistry ID 0x%04x", val); in bq274xx_ensure_chemistry()
378 return -EINVAL; in bq274xx_ensure_chemistry()
383 LOG_ERR("Unable to configure chemistry"); in bq274xx_ensure_chemistry()
384 return -EIO; in bq274xx_ensure_chemistry()
390 static int bq274xx_gauge_configure(const struct device *dev) in bq274xx_gauge_configure()
392 const struct bq274xx_config *const config = dev->config; in bq274xx_gauge_configure()
393 struct bq274xx_data *data = dev->data; in bq274xx_gauge_configure()
401 if (data->regs == NULL) { in bq274xx_gauge_configure()
406 LOG_ERR("Unable to get device ID"); in bq274xx_gauge_configure()
407 return -EIO; in bq274xx_gauge_configure()
411 data->regs = &bq27421_regs; in bq274xx_gauge_configure()
413 data->regs = &bq27427_regs; in bq274xx_gauge_configure()
415 LOG_ERR("Unsupported device ID: 0x%04x", id); in bq274xx_gauge_configure()
416 return -ENOTSUP; in bq274xx_gauge_configure()
419 regs = data->regs; in bq274xx_gauge_configure()
421 designenergy_mwh = (uint32_t)config->design_capacity * 37 / 10; /* x3.7 */ in bq274xx_gauge_configure()
422 taperrate = config->design_capacity * 10 / config->taper_current; in bq274xx_gauge_configure()
427 return -EIO; in bq274xx_gauge_configure()
433 return -EIO; in bq274xx_gauge_configure()
441 ret = i2c_reg_write_byte_dt(&config->i2c, BQ274XX_EXT_DATA_CONTROL, 0x00); in bq274xx_gauge_configure()
444 return -EIO; in bq274xx_gauge_configure()
453 regs->dm_design_capacity, config->design_capacity, in bq274xx_gauge_configure()
456 regs->dm_design_energy, designenergy_mwh, in bq274xx_gauge_configure()
459 regs->dm_terminate_voltage, config->terminate_voltage, in bq274xx_gauge_configure()
462 regs->dm_taper_rate, taperrate, in bq274xx_gauge_configure()
473 if (data->regs == &bq27427_regs) { in bq274xx_gauge_configure()
494 return -EIO; in bq274xx_gauge_configure()
500 return -EIO; in bq274xx_gauge_configure()
503 data->configured = true; in bq274xx_gauge_configure()
508 static int bq274xx_channel_get(const struct device *dev, enum sensor_channel chan, in bq274xx_channel_get()
511 struct bq274xx_data *data = dev->data; in bq274xx_channel_get()
516 val->val1 = ((data->voltage / 1000)); in bq274xx_channel_get()
517 val->val2 = ((data->voltage % 1000) * 1000U); in bq274xx_channel_get()
521 val->val1 = ((data->avg_current / 1000)); in bq274xx_channel_get()
522 val->val2 = ((data->avg_current % 1000) * 1000U); in bq274xx_channel_get()
526 val->val1 = ((data->stdby_current / 1000)); in bq274xx_channel_get()
527 val->val2 = ((data->stdby_current % 1000) * 1000U); in bq274xx_channel_get()
531 val->val1 = ((data->max_load_current / 1000)); in bq274xx_channel_get()
532 val->val2 = ((data->max_load_current % 1000) * 1000U); in bq274xx_channel_get()
537 int_temp = data->internal_temperature * 10; in bq274xx_channel_get()
539 int_temp -= (int32_t)(100.0 * KELVIN_OFFSET); in bq274xx_channel_get()
540 val->val1 = int_temp / 100; in bq274xx_channel_get()
541 val->val2 = (int_temp % 100) * 10000; in bq274xx_channel_get()
545 val->val1 = data->state_of_charge; in bq274xx_channel_get()
546 val->val2 = 0; in bq274xx_channel_get()
550 val->val1 = data->state_of_health; in bq274xx_channel_get()
551 val->val2 = 0; in bq274xx_channel_get()
555 val->val1 = data->full_charge_capacity; in bq274xx_channel_get()
556 val->val2 = 0; in bq274xx_channel_get()
560 val->val1 = data->remaining_charge_capacity; in bq274xx_channel_get()
561 val->val2 = 0; in bq274xx_channel_get()
565 val->val1 = data->nom_avail_capacity; in bq274xx_channel_get()
566 val->val2 = 0; in bq274xx_channel_get()
570 val->val1 = data->full_avail_capacity; in bq274xx_channel_get()
571 val->val2 = 0; in bq274xx_channel_get()
575 val->val1 = data->avg_power; in bq274xx_channel_get()
576 val->val2 = 0; in bq274xx_channel_get()
580 return -ENOTSUP; in bq274xx_channel_get()
586 static int bq274xx_sample_fetch(const struct device *dev, enum sensor_channel chan) in bq274xx_sample_fetch()
588 struct bq274xx_data *data = dev->data; in bq274xx_sample_fetch()
589 int ret = -ENOTSUP; in bq274xx_sample_fetch()
591 if (!data->configured) { in bq274xx_sample_fetch()
600 &data->voltage); in bq274xx_sample_fetch()
603 return -EIO; in bq274xx_sample_fetch()
609 &data->avg_current); in bq274xx_sample_fetch()
612 return -EIO; in bq274xx_sample_fetch()
618 &data->internal_temperature); in bq274xx_sample_fetch()
621 return -EIO; in bq274xx_sample_fetch()
627 &data->stdby_current); in bq274xx_sample_fetch()
630 return -EIO; in bq274xx_sample_fetch()
636 &data->max_load_current); in bq274xx_sample_fetch()
639 return -EIO; in bq274xx_sample_fetch()
645 &data->state_of_charge); in bq274xx_sample_fetch()
648 return -EIO; in bq274xx_sample_fetch()
654 &data->full_charge_capacity); in bq274xx_sample_fetch()
657 return -EIO; in bq274xx_sample_fetch()
663 &data->remaining_charge_capacity); in bq274xx_sample_fetch()
666 return -EIO; in bq274xx_sample_fetch()
672 &data->nom_avail_capacity); in bq274xx_sample_fetch()
675 return -EIO; in bq274xx_sample_fetch()
681 &data->full_avail_capacity); in bq274xx_sample_fetch()
684 return -EIO; in bq274xx_sample_fetch()
690 &data->avg_power); in bq274xx_sample_fetch()
693 return -EIO; in bq274xx_sample_fetch()
699 &data->state_of_health); in bq274xx_sample_fetch()
701 data->state_of_health = (data->state_of_health) & 0x00FF; in bq274xx_sample_fetch()
705 return -EIO; in bq274xx_sample_fetch()
717 static int bq274xx_gauge_init(const struct device *dev) in bq274xx_gauge_init()
719 const struct bq274xx_config *const config = dev->config; in bq274xx_gauge_init()
722 if (!device_is_ready(config->i2c.bus)) { in bq274xx_gauge_init()
723 LOG_ERR("I2C bus device not ready"); in bq274xx_gauge_init()
724 return -ENODEV; in bq274xx_gauge_init()
728 if (!gpio_is_ready_dt(&config->int_gpios)) { in bq274xx_gauge_init()
729 LOG_ERR("GPIO device pointer is not ready to be used"); in bq274xx_gauge_init()
730 return -ENODEV; in bq274xx_gauge_init()
742 if (!config->lazy_loading) { in bq274xx_gauge_init()
750 static int bq274xx_enter_shutdown_mode(const struct device *dev) in bq274xx_enter_shutdown_mode()
787 static int bq274xx_exit_shutdown_mode(const struct device *dev) in bq274xx_exit_shutdown_mode()
789 const struct bq274xx_config *const config = dev->config; in bq274xx_exit_shutdown_mode()
792 ret = gpio_pin_configure_dt(&config->int_gpios, GPIO_OUTPUT | GPIO_OPEN_DRAIN); in bq274xx_exit_shutdown_mode()
798 ret = gpio_pin_set_dt(&config->int_gpios, 0); in bq274xx_exit_shutdown_mode()
806 ret = gpio_pin_configure_dt(&config->int_gpios, GPIO_INPUT); in bq274xx_exit_shutdown_mode()
812 if (!config->lazy_loading) { in bq274xx_exit_shutdown_mode()
825 static int bq274xx_pm_action(const struct device *dev, in bq274xx_pm_action()
838 ret = -ENOTSUP; in bq274xx_pm_action()