@@ -25,9 +25,10 @@ LOG_MODULE_REGISTER(max17055, CONFIG_SENSOR_LOG_LEVEL);
2525 * @param valp Place to put the value on success
2626 * @return 0 if successful, or negative error code from I2C API
2727 */
28- static int max17055_reg_read (struct max17055_data * priv , int reg_addr ,
28+ static int max17055_reg_read (const struct device * dev , int reg_addr ,
2929 int16_t * valp )
3030{
31+ struct max17055_data * priv = dev -> data ;
3132 uint8_t i2c_data [2 ];
3233 int rc ;
3334
@@ -41,9 +42,10 @@ static int max17055_reg_read(struct max17055_data *priv, int reg_addr,
4142 return 0 ;
4243}
4344
44- static int max17055_reg_write (struct max17055_data * priv , int reg_addr ,
45+ static int max17055_reg_write (const struct device * dev , int reg_addr ,
4546 uint16_t val )
4647{
48+ struct max17055_data * priv = dev -> data ;
4749 uint8_t buf [3 ];
4850
4951 buf [0 ] = (uint8_t )reg_addr ;
@@ -241,7 +243,7 @@ static int max17055_sample_fetch(const struct device *dev,
241243 for (size_t i = 0 ; i < ARRAY_SIZE (regs ); i ++ ) {
242244 int rc ;
243245
244- rc = max17055_reg_read (priv , regs [i ].reg_addr , regs [i ].dest );
246+ rc = max17055_reg_read (dev , regs [i ].reg_addr , regs [i ].dest );
245247 if (rc != 0 ) {
246248 LOG_ERR ("Failed to read channel %d" , chan );
247249 return rc ;
@@ -251,26 +253,27 @@ static int max17055_sample_fetch(const struct device *dev,
251253 return 0 ;
252254}
253255
254- static int max17055_exit_hibernate (struct max17055_data * priv )
256+ static int max17055_exit_hibernate (const struct device * dev )
255257{
256258 LOG_DBG ("Exit hibernate" );
257259
258- if (max17055_reg_write (priv , SOFT_WAKEUP , SOFT_WAKEUP_WAKEUP )) {
260+ if (max17055_reg_write (dev , SOFT_WAKEUP , SOFT_WAKEUP_WAKEUP )) {
259261 return - EIO ;
260262 }
261- if (max17055_reg_write (priv , HIB_CFG , HIB_CFG_CLEAR )) {
263+ if (max17055_reg_write (dev , HIB_CFG , HIB_CFG_CLEAR )) {
262264 return - EIO ;
263265 }
264- if (max17055_reg_write (priv , SOFT_WAKEUP , SOFT_WAKEUP_CLEAR )) {
266+ if (max17055_reg_write (dev , SOFT_WAKEUP , SOFT_WAKEUP_CLEAR )) {
265267 return - EIO ;
266268 }
267269
268270 return 0 ;
269271}
270272
271- static int max17055_write_config (struct max17055_data * priv ,
272- const struct max17055_config * const config )
273+ static int max17055_write_config (const struct device * dev )
273274{
275+ const struct max17055_config * config = dev -> config ;
276+
274277 uint16_t design_capacity = capacity_to_max17055 (config -> rsense_mohms ,
275278 config -> design_capacity );
276279 uint16_t d_qacc = design_capacity / 32 ;
@@ -282,53 +285,52 @@ static int max17055_write_config(struct max17055_data *priv,
282285 LOG_DBG ("DesignCap: %u, dQAcc: %u, IChgTerm: %u, VEmpty: %u, dPAcc: %u" ,
283286 design_capacity , d_qacc , i_chg_term , v_empty , d_pacc );
284287
285- if (max17055_reg_write (priv , DESIGN_CAP , design_capacity )) {
288+ if (max17055_reg_write (dev , DESIGN_CAP , design_capacity )) {
286289 return - EIO ;
287290 }
288- if (max17055_reg_write (priv , D_QACC , d_qacc )) {
291+ if (max17055_reg_write (dev , D_QACC , d_qacc )) {
289292 return - EIO ;
290293 }
291- if (max17055_reg_write (priv , ICHG_TERM , i_chg_term )) {
294+ if (max17055_reg_write (dev , ICHG_TERM , i_chg_term )) {
292295 return - EIO ;
293296 }
294- if (max17055_reg_write (priv , V_EMPTY , v_empty )) {
297+ if (max17055_reg_write (dev , V_EMPTY , v_empty )) {
295298 return - EIO ;
296299 }
297- if (max17055_reg_write (priv , D_PACC , d_pacc )) {
300+ if (max17055_reg_write (dev , D_PACC , d_pacc )) {
298301 return - EIO ;
299302 }
300- if (max17055_reg_write (priv , MODEL_CFG , MODELCFG_REFRESH )) {
303+ if (max17055_reg_write (dev , MODEL_CFG , MODELCFG_REFRESH )) {
301304 return - EIO ;
302305 }
303306
304307 uint16_t model_cfg = MODELCFG_REFRESH ;
305308
306309 while (model_cfg & MODELCFG_REFRESH ) {
307- max17055_reg_read (priv , MODEL_CFG , & model_cfg );
310+ max17055_reg_read (dev , MODEL_CFG , & model_cfg );
308311 k_sleep (K_MSEC (10 ));
309312 }
310313
311314 return 0 ;
312315}
313316
314- static int max17055_init_config (struct max17055_data * priv ,
315- const struct max17055_config * const config )
317+ static int max17055_init_config (const struct device * dev )
316318{
317319 int16_t hib_cfg ;
318320
319- if (max17055_reg_read (priv , HIB_CFG , & hib_cfg )) {
321+ if (max17055_reg_read (dev , HIB_CFG , & hib_cfg )) {
320322 return - EIO ;
321323 }
322324
323- if (max17055_exit_hibernate (priv )) {
325+ if (max17055_exit_hibernate (dev )) {
324326 return - EIO ;
325327 }
326328
327- if (max17055_write_config (priv , config )) {
329+ if (max17055_write_config (dev )) {
328330 return - EIO ;
329331 }
330332
331- if (max17055_reg_write (priv , HIB_CFG , hib_cfg )) {
333+ if (max17055_reg_write (dev , HIB_CFG , hib_cfg )) {
332334 return - EIO ;
333335 }
334336
@@ -354,7 +356,7 @@ static int max17055_gauge_init(const struct device *dev)
354356 return - EINVAL ;
355357 }
356358
357- if (max17055_reg_read (priv , STATUS , & tmp )) {
359+ if (max17055_reg_read (dev , STATUS , & tmp )) {
358360 return - EIO ;
359361 }
360362
@@ -366,20 +368,20 @@ static int max17055_gauge_init(const struct device *dev)
366368 /* Wait for FSTAT_DNR to be cleared */
367369 tmp = FSTAT_DNR ;
368370 while (tmp & FSTAT_DNR ) {
369- max17055_reg_read (priv , FSTAT , & tmp );
371+ max17055_reg_read (dev , FSTAT , & tmp );
370372 }
371373
372- if (max17055_init_config (priv , config )) {
374+ if (max17055_init_config (dev )) {
373375 return - EIO ;
374376 }
375377
376378 /* Clear PowerOnReset bit */
377- if (max17055_reg_read (priv , STATUS , & tmp )) {
379+ if (max17055_reg_read (dev , STATUS , & tmp )) {
378380 return - EIO ;
379381 }
380382
381383 tmp &= ~STATUS_POR ;
382- return max17055_reg_write (priv , STATUS , tmp );
384+ return max17055_reg_write (dev , STATUS , tmp );
383385}
384386
385387static const struct sensor_driver_api max17055_battery_driver_api = {
0 commit comments