Here's the ninth post in the Build-a-Driver series for MCP9808.
(If this post seems out of context, please refer to "Build-a-Driver Introduction" post.)
MCP9808 powers on in continuous conversion mode. It is continuously taking temperature measurements. If the device was to be deleted from the system, without power be removed, it would be nice to go into a low-power mode until/unless the device gets added to the system again.
Note: this won't be suspend/resume operations, but simply shutdown on device deletion, and wake-up on device addition.
From MCP9808 Datasheet:
The driver will execute shutdown mode when a device is deleted.
Example: # echo 0x50 > /sys/bus/i2c/devices/i2c-3/delete_device
The driver will put the sensor into shutdown/low-power mode. In shutdown mode, the devices registers are all readable, but it is no longer measuring temperature, hence the alias 'low-power mode'.
Now, once the sensor is put into shutdown mode it stays that way until the bit is cleared via a power-cycle, or, as in this case, it gets probed again. We need to add some initialization and wake-up code to the probe routine to get it back to the expected power-on defaults. Previously, it was assumed that the sensor was freshly powered-up. Now, we see that may not always be the case.
- define the sensors config register; first time needing it
- define config default and a config shutdown register values
- in probe set the config register to the config power on default
- in probe set the resolution register to the power on default
- in probe remove the initialization of data->int_time, since *set_it_time() will now do it.
- add .remove function that un-registers the device and then writes the shutdown bit to sensor
- tell i2c about our new .remove function via the i2c_driver definition
Verified the shutdown by leaving out the probe wake-up changes and confirmed that the registers were all still readable, yet the temperature data was not refreshing. Verified the probe wake-up changes by cycling in/out of shutdown via instantiating new_device/delete_device.
Labels: Build-a-Driver, MCP9808, Temperature Sensor