Post AGTgLHV5EROv1Wb8SW by calebccff@fosstodon.org
(DIR) More posts by calebccff@fosstodon.org
(DIR) Post #AGTg7maoplerOvqOh6 by calebccff@fosstodon.org
2022-02-14T19:54:56Z
0 likes, 0 repeats
what a day, I realised that some function I wrote was actually very naive and not properly tested, so I've been rewriting it... Lets dig in a little, I'm working on a driver for the "RRADC", it lives on the PMIC, connected to the SoC via SPMI System Power Management Interface ๐งต
(DIR) Post #AGTg9k1xKcGG28THI8 by calebccff@fosstodon.org
2022-02-14T19:54:59Z
0 likes, 0 repeats
The Qcom PMIC is actually two SPMI devices, I guess that's one way to increase the address space. So "function" devices, like ADCs, controllers, battery chargers etc exist on the PMIC - on one of it's USIDs (unique... device identifiers), below is a DT snippet for the RRADC.
(DIR) Post #AGTg9lzC3rEw69C6GO by calebccff@fosstodon.org
2022-02-14T19:54:59Z
0 likes, 0 repeats
The RRADC has some special calibration it uses for some of the channels, depends on which chip fab the PMIC was manufactured in the readings can be a bit different, so it compensates with some fixed per-PMIC adjustments. I didn't think this would be too bad (FORESHADOWING!!!)
(DIR) Post #AGTg9o0KYbL0MFk2JU by calebccff@fosstodon.org
2022-02-14T19:55:05Z
0 likes, 0 repeats
The upstream Qcom PMIC driver doesn't expose any of the revision information (revid) such as the PMIC model, version etc to other drivers, so I had to add that code, this data was already read but just unused.
(DIR) Post #AGTg9qQdXlZJsQ44au by calebccff@fosstodon.org
2022-02-14T19:55:05Z
0 likes, 0 repeats
So, all done right? But hang on, how do you actually get the data in the context of the RRADC driver (or any function driver for that matter)? Well that's where it gets tricky, and we have to actually learn a bit about kernel frameworks (blergh).
(DIR) Post #AGTgJofYwIiFJfnieu by calebccff@fosstodon.org
2022-02-14T19:57:10Z
0 likes, 0 repeats
My original solution here, was to set the SPMI driver data to the PMIC revid, as it was unused, and then simply pull it out in the function driver by calling dev_get_drvdata() for the parent device. This works... mostly, but ignores the multiple USID issue...
(DIR) Post #AGTgLHV5EROv1Wb8SW by calebccff@fosstodon.org
2022-02-14T19:57:10Z
0 likes, 0 repeats
As you can see from the snippet above, we only read the PMIC revid for the first USID, and there can be two. So for function devices on the other USID the data would be NULL :(Now, at first glance this doesn't seem toooo bad, and, in theory it isn't
(DIR) Post #AGTgLJHgbDsAYYLSr2 by calebccff@fosstodon.org
2022-02-14T19:57:13Z
0 likes, 0 repeats
at least, if you know what you're doing.So here's what I ended up with, I'll show it all and then we can walk through it
(DIR) Post #AGTgLLDrOQ06ZGZRAW by calebccff@fosstodon.org
2022-02-14T19:57:15Z
0 likes, 0 repeats
So as it says at the top, the function takes a device pointer (the device associated with the function driver) and returns the PMIC revid. It first checks that the device is actually from a function device and not something else.
(DIR) Post #AGTgLNHpicMoyARddg by calebccff@fosstodon.org
2022-02-14T19:57:17Z
0 likes, 0 repeats
Then it checks for the shortcut where the function device is on the base USID, this is actually the codepath the RRADC hits. If it is then we just return the driverdata and all is golden.
(DIR) Post #AGTga9qDzS9Fc5a2Pw by calebccff@fosstodon.org
2022-02-14T20:00:05Z
0 likes, 0 repeats
If not.. things get tricky! We now need to find the USID before us in the device heirachy, and what's the best way to peruse the device heirachy?! Why DeviceTree of course (usually).We start by getting the first PMIC as well as the SPMI bus device node
(DIR) Post #AGTgadh308dICJW8HI by calebccff@fosstodon.org
2022-02-14T20:00:07Z
0 likes, 0 repeats
Now we can walk through every PMIC/USID, check it's USID (that's what the of_property_read_u32_array does), and then get the SPMI device associated with it. The spmi_device_from_of function was also added by me.
(DIR) Post #AGTgaiSLIEqCy9fxYG by calebccff@fosstodon.org
2022-02-14T20:00:09Z
0 likes, 0 repeats
If there is no SPMI device yet, that means it hasn't probed yet. If there is no SPMI device and we're at the right USID (we can check by reading reg) then we have to return -EPROBE_DEFER and have the function driver probe again later, when hopefully the base USID has probed.
(DIR) Post #AGTgakZrPG2jY3CzY0 by calebccff@fosstodon.org
2022-02-14T20:00:11Z
0 likes, 0 repeats
If the SPMI device is valid, then we check to see if it's USID is one less than the USID of the function device parent (ie, it's the base USID for the PMIC the function driver is on), and return it's driver data in that case.
(DIR) Post #AGTgamkZKPnUHqEZW4 by calebccff@fosstodon.org
2022-02-14T20:00:13Z
0 likes, 0 repeats
If it's not, then we continue the loop by getting the next PMIC USID. The while loop is a little cheeky, it lets us avoid a null check as we know we never want to run the loop on an odd-numbered USID
(DIR) Post #AGTgrSs6UOD1YdiBoe by calebccff@fosstodon.org
2022-02-14T20:03:14Z
0 likes, 0 repeats
if the sibling is null then that means this is the last USID, and because they come in pairs that means it's an odd number and therefore not the base USID, so we can skip it.
(DIR) Post #AGTgrUoHHaKxZLwA88 by calebccff@fosstodon.org
2022-02-14T20:03:15Z
0 likes, 0 repeats
I will actually rework this part because it's quite obscure and could lead to bugs in the future by implicitly depending too much on how the PMIC hw is designed.Anyway, I hope you enjoyed this thread, please drop a follow if so!
(DIR) Post #AGVZ9qGZptug0iKZaS by ben@cawfee.club
2022-02-14T21:21:08.630433Z
0 likes, 0 repeats
@calebccff side question, what code editor are you using
(DIR) Post #AGVZ9qnBse07dracQC by calebccff@fosstodon.org
2022-02-15T17:46:22Z
0 likes, 0 repeats
@ben i use vscode (yes terrible i know ๐) with the "monakai charcoal full dark" theme.
(DIR) Post #AGwbKFZP32XYar0RFY by ben@cawfee.club
2022-02-19T11:08:20.299609Z
0 likes, 0 repeats
@calebccff do you use vscode (from MS) or vscodium? I tried to install that theme on vscodium, couldn't find it there, tried to use the cli, it says not found.
(DIR) Post #AGwbKG1lLbE20oH5SC by calebccff@fosstodon.org
2022-02-28T18:47:52Z
0 likes, 0 repeats
@ben i use M$ vscode