Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: "Grönke, Christian" <C.Groenke@infodas.de>
Cc: "linux-hwmon@vger.kernel.org" <linux-hwmon@vger.kernel.org>
Subject: Re: PMBus driver for FSP/3Y Power device with non-standard VOUT values (LINEAR11 vs LINEAR16)
Date: Fri, 15 Mar 2019 12:38:59 -0700	[thread overview]
Message-ID: <20190315193859.GA10792@roeck-us.net> (raw)
In-Reply-To: <20190315182945.GB4313@roeck-us.net>

On Fri, Mar 15, 2019 at 11:29:45AM -0700, Guenter Roeck wrote:
> On Fri, Mar 15, 2019 at 10:19:33AM +0000, Grönke, Christian wrote:
> > Hello Guenter,
> > 
> > > -----Ursprüngliche Nachricht-----
> > > Von: Guenter Roeck <groeck7@gmail.com> Im Auftrag von Guenter Roeck
> > > Gesendet: Donnerstag, 14. März 2019 17:53
> > > An: Grönke, Christian <C.Groenke@infodas.de>
> > > Cc: linux-hwmon@vger.kernel.org
> > > Betreff: Re: PMBus driver for FSP/3Y Power device with non-standard VOUT
> > > values (LINEAR11 vs LINEAR16)
> > > 
> > > Hi Christian,
> > > On Thu, Mar 14, 2019 at 04:08:32PM +0000, Grönke, Christian wrote:
> > > >
> > > > The framework code seemed to work fine. I used your code for the
> > > conversion:
> > > >     linear11 -> 'scaled integer' -> ieee754 It provided a way to test
> > > > the code and was easy for me as my tries to do some other bit magic
> > > > weren't successful. That means I partly tested the code from
> > > > pmbus_data2reg_ieee754 as my read_word function uses this for the
> > > > conversion. Of course not the module local function...
> > > >
> > > 
> > > Wondering ... I would have thought that it should be possible to
> > > implement a simplified linear11 <--> ieee754 conversion, without
> > > converting to a scaled integer first. Have you tried that ?
> > 
> > There might be a more efficient and elegant way. I toyed around with
> > the conversion for a moment but didn't figure out a working way 
> > quickly.
> 
> This should work.
> 
> u16 lin2ieee(u16 lin11)
> {
> 	s16 mantissa = ((s16)((lin11 & 0x7ff) << 5)) >> 5;
> 	int exponent = (((s16)lin11) >> 11) + 25;
> 	u16 sign = 0;
> 
> 	/* simple case */
> 	if (mantissa == 0)
> 		return 0;
> 
> 	if (mantissa < 0) {
> 		sign = 0x8000;
> 		mantissa = -mantissa;
> 	}
> 
> 	while (mantissa < 0x400 && exponent > 1) {
> 		mantissa <<= 1;
> 		exponent--;
> 	}
> 
> 	/* boundary checks */
> 	if (exponent > 30)
> 		return sign | 0x7bff;
> 	if (mantissa < 0x400)
> 		return sign | 0x0400;
> 
> 	return sign | (exponent << 10) | (mantissa & 0x3ff);
> }
> 
Plus, just in case, the other direction.

u16 ieee2lin(u16 ieee)
{
	s16 mantissa = (ieee & 0x3ff) + 0x400;
	int exponent = ((ieee >> 10) & 0x1f) - 25; 
	int sign = ieee & 0x8000;

	/* simple case */
	if (ieee == sign)
		return 0;

	/* map into lin11 number space */
	mantissa >>= 1;
	exponent++;

	while (mantissa && exponent < -15) {
		exponent++;
		mantissa >>= 1;
	}

	/* boundary checks */
	if (exponent < -15)	/* number too small */
		return 0;

	if (sign)
		mantissa = -mantissa;

	return (exponent << 11) | (mantissa & 0x7ff);
}

Guenter

  reply	other threads:[~2019-03-15 19:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-13 12:31 PMBus driver for FSP/3Y Power device with non-standard VOUT values (LINEAR11 vs LINEAR16) Grönke, Christian
2019-03-13 15:19 ` Guenter Roeck
2019-03-13 16:20   ` AW: " Grönke, Christian
2019-03-13 16:30     ` Guenter Roeck
2019-03-13 17:35       ` AW: " Grönke, Christian
2019-03-14  3:18         ` Guenter Roeck
2019-03-14 16:08           ` AW: " Grönke, Christian
2019-03-14 16:22             ` Guenter Roeck
2019-03-14 16:53             ` Guenter Roeck
2019-03-15 10:19               ` AW: " Grönke, Christian
2019-03-15 18:29                 ` Guenter Roeck
2019-03-15 19:38                   ` Guenter Roeck [this message]
2019-03-16 15:27                     ` Guenter Roeck
2019-03-13 16:21   ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190315193859.GA10792@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=C.Groenke@infodas.de \
    --cc=linux-hwmon@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox